这看起来很基本,但我无法弄明白。
我有一个“撰写评论”链接,如下所示:
<%= link_to 'Write', new_review_path(@new, :vendor_id => @vendor.id) %>
这会创建一个URL:
评论/新?VENDOR_ID = 10
我想要的只是基于三个输入创建一个新的Review对象:
但我无法存储vendor_id:
def new
@review = Review.new
@vendor = Vendor.find(params[:vendor_id])
@vendor.reviews.create
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @review }
end
end
def create
@review = Review.new(params[:review])
#@vendor = Vendor.find(params[:vendor_id]) #error
#@review = @vendor.reviews.build #error
@review = @current_user.reviews.build #build a review with the current_user id
respond_to do |format|
if @review.save
flash[:notice] = 'Review was successfully created.'
format.html { redirect_to(@review) }
format.xml { render :xml => @review, :status => :created, :location => @review }
else
format.html { render :action => "new" }
format.xml { render :xml => @review.errors, :status => :unprocessable_entity }
end
end
end
我在想我需要更改form_for中的url,但我不清楚如何。这是我目前的情况:
<% form_for(@review) do |f| %>
......
我可以帮忙吗?谢谢!
这是new的视图代码(注意:我还创建了一个has_many路由,这样vendor / 1 / reviews / new就有意义了,虽然我不再调用它了):
我在routes.rb中的代码:
map.resources :vendors, :has_many => :reviews
我的观看/评论/ new.html.erb代码:
<h1>New review for <%= @vendor.name%></h1>
Current User ID: <%= @current_user.id %>
Vendor ID: <%= @vendor.id %> <%= @review.vendor.id %>
Current Review ID: <%= @review.id %>
<% form_for @review do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :summary %><br />
<%= f.text_field :summary %><br />
<%= f.label :pro_review %><br />
<%= f.text_field :pro_review %><br />
<%= f.label :con_review %><br />
<%= f.text_field :con_review %><br />
<%= f.label :detail_review %><br />
<%= f.text_field :detail_review %> <br />
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', reviews_path %>
以下是源视图:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Reviews: new</title>
<link href="/stylesheets/scaffold.css?1238999620" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<p style="color: green"></p>
<h1>New review for Fiberlink</h1>
Current User ID: 2
Vendor ID: 16 16
Current Review ID:
<form action="/reviews" class="new_review" id="new_review" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="XDknE1mvQT4zwem1z/gCYm8I2ODhqHLKKgd8y12zzzo=" /></div>
<p>
<label for="review_summary">Summary</label><br />
<input id="review_summary" name="review[summary]" size="30" type="text" /><br />
<label for="review_pro_review">Pro review</label><br />
<input id="review_pro_review" name="review[pro_review]" size="30" type="text" /><br />
<label for="review_con_review">Con review</label><br />
<input id="review_con_review" name="review[con_review]" size="30" type="text" /><br />
<label for="review_detail_review">Detail review</label><br />
<input id="review_detail_review" name="review[detail_review]" size="30" type="text" /> <br />
</p>
<p>
<input id="review_submit" name="commit" type="submit" value="Create" />
</p>
</form>
<a href="/reviews">Back</a>
</body>
</html>
答案 0 :(得分:2)
你快到了。您需要稍微更改new
方法。
def new
@review = Review.new
@vendor = Vendor.find(params[:vendor_id])
@review.vendor = @vendor # this will insert the vendor_id into the form
# when you do form_for
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @review }
end
end
编辑:尝试在视图代码中添加隐藏字段以保留vendor_id
。
<% form_for @review do |f| %>
<%= f.hidden_field vendor_id %>
# ...
编辑2:实际上,查看代码生成的HTML,我认为您需要稍微不同form_for
。正如你所知,Rails的UI方面并不是我的强项。 :)
<% form_for :review, @review, :url => { :action => "create" } do |f| %>
<%= f.hidden_field vendor_id %>
# ...
答案 1 :(得分:1)
我通常会这样做:
<% form_for Review.new, :url => new_review_path do |f| %>
<%= f.hidden_field :vendor_id, :value => @vendor.id %>