2011-11-14 7 views
1

이전에는 작동했지만 왜 지금은 작동하지 않습니까? SignIn 사용자 만 게시물을 만들려고했습니다. 아니면 내 라우팅 문제입니까? 내 라우팅 자원이다 : microposts 끝정의되지 않은 메소드`microposts '

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" %> 

microposts 모델

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 
: 사용자가 자원을

마이그레이션 microposts

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 
관련 문제