1
以前はうまくいったのですが、今はなぜですか?私はサインインユーザーだけで投稿を作成しようとしていました。それは私のルーティングの問題ですか? 私のルーティングが 資源である:マイクロポスト エンド未定義のメソッド `microposts '
マイクロポストコントローラ
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Post created!"
redirect_to @micropost
else
render 'new'
end
end
new.html.erb
<%= form_for @micropost do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.label :title %><br />
<%=h f.text_field :title %><br />
<%= f.label :content %><br />
<%=h f.text_area :content, :row => 30, :cols=> 30 %><br />
<%= f.label :category %><br />
<%=h f.text_field :category %><br />
<%= f.submit "Post" %>
マイクロポストモデル
class Micropost < ActiveRecord::Base
belongs_to :users
default_scope :order => 'microposts.created_at DESC'
attr_accessible :title,:content,:category
validates :user_id, :presence => true
validates :title, :presence => true,
:length => {:maximum =>500}
validates :content, :presence => true,
:length => {:maximum =>3000}
validates :category, :presence => true
end
:ユーザーが リソースを行いますの
移行マイクロポスト
class CreateMicroposts < ActiveRecord::Migration
def self.up
create_table :microposts do |t|
t.string :title
t.string :content
t.string :user_id
t.string :category
t.timestamps
end
add_index :microposts, [:title, :created_at, :category]
end
def self.down
drop_table :microposts
end
end
はどこUserモデルですか?それに 'has_many:microposts'がありますか? – jer