2011-11-14 30 views
1

它工作過,但它爲什麼不是現在?我試圖通過signIn用戶創建一個帖子。或者是我的路由問題? 我的路由 資源:用戶做 資源:微柱 結束未定義的方法`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 
+1

哪裏是你的用戶模式?你有一個'has_many:microposts'嗎? – jer

回答

5

你的用戶模型的需求:

has_many :microposts