2011-12-14 120 views
0

我的场景:电影有评论,评论有评论。Rails3路由错误

电影模式:

has_many :reviews 

审查模式:

has_many :comments 
belongs_to :movie 

评价模型:

belongs_to :review 

路线:

resources :movies do 
    resources :reviews do 
    resources :comments 
    end 
end 

评论控制器:

def create 
    @movie = Movie.find(params[:movie_id]) 
    @review = Review.where(:movie_id => @movie.id) 
    @comment = @review.comments.create(params[:comment]) // Line 5 
    redirect_to movie_path(@movie) 
end 

评论观点:

<%= form_for([@movie, r, r.comments.build]) do |f| %> 
    <div class="field"> 
    <%= f.text_area :body %> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Submit" %> 
    </div> 
<% end %> 

,我得到的是错误:

NoMethodError (undefined method `comments' for #<ActiveRecord::Relation:0x007ff5c5870010>): 
app/controllers/comments_controller.rb:5:in `create' 

有人能告诉我什么,我做错了什么?

在此先感谢..

回答

2

Review.where收益评价的列表,你想要的是一个实例

@review = Review.where(:movie_id => @movie.id).first 

@review = Review.find_by_movie_id(@movie.id) 

确保处理nil情况。

+0

完美的作品..(我刚刚接受了另一个问题的答案,它让我等待4分钟)..将在4 ..后接受.. ..非常感谢 – Ari53nN3o 2011-12-14 10:48:54