2011-12-14 121 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