ネストされたリソースへのコメントを許可しようとしていますが、[コメントの作成]ボタンを選択すると、app / controllers / comments_controller.rb:7:in `create'の#に対して未定義のメソッド'comment'が表示されます。私はまだrubyとrailsに慣れていないので、スタートガイドのコードに従いましたが、エラーの理由がわからないようです。
関連付けは次のとおりです。
has_many :comments
belongs_to :restaurant
私のルートで
resources :restaurants do
resources :comments
end
コメントコントローラーで
def create
@restaurant = Restaurant.find(params[:restaurant_id])
@comment = @restaurant.comments.create(params[:comment])
redirect_to restaurant_path
end
私のレストランのショーテンプレートで
<%= form_for([@restaurant, @restaurant.comments.build]) do |f| %>
<h2>Add a comment</h2>
<div>
<%= f.text_area :body %>
</div>
<div>
<%= f.submit %>
<% end %>