未定义的方法`microposts’
它以前工作但为什么现在不是? 我试图只通过signIn用户创建一个post。或者是我的路由问题? 我的路由是资源:用户做资源:微博结束
微博控制器
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
f.object %> current_user.id %>
30, :cols=> 30 %>
微博模型
class Micropost '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
您的用户模型需要:
has_many :microposts