我有2张桌子:场地和评论,其中1个场地可以有很多评论,评论属于一个场地。我目前通过场地展示页面中的链接添加评论到评论新页面:
地点/ show.html.erb
<div class="button">
<%= link_to 'Add a review', new_venue_review_path(@venue) %>
</div>
的评价/ new.html.erb
<% form_for [@venue, @review] do |f| %>
<p>title: <br>
<%= f.text_field :title %></p>
<%= submit_tag %>
<% end %>
评论控制器
def new
@review = Review.new
end
def create
@review = Review.new params[:review]
@review.venue = @venue
if @review.save
flash[:notice] = 'Review added'
redirect_to venue_path(@venue)
else
render :action => :new
end
end
场地管制员
class VenuesController < ApplicationController
def index
if
@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas])
else
@venues = Venue.all
end
end
def new
@venue = Venue.new
end
def create
@venue = Venue.new params[:venue]
if @venue.save
flash[:notice] = 'Venue added'
redirect_to venues_path
else
render :action => :new
end
end
def edit
@venue = Venue.find(params[:id])
end
def update
@venue = Venue.find(params[:id])
@venue.attributes = params[:venue]
if @venue.save!
flash[:notice] = 'Venue updated successfully'
redirect_to venues_path(@venue)
end
end
def show
@venue = Venue.find(params[:id])
end
end
路由
resources :venues do
resources :reviews
end
end
如何将添加新评论表单直接移至场地展示页面?移动代码为NilClass提供了一个未定义的方法`model_name':Class error。
感谢您的帮助!
答案 0 :(得分:2)
只需将@review = Review.new
添加到场地控制器的show动作中即可。然后将表格放在场地#show view file中。
<强> venues_controller.rb 强>
def show
@venue = Venue.find(params[:id])
@review = Review.new
end