這似乎很基本,但我無法弄清楚。我如何在控制器之間傳遞一個值(新建 - >創建)RoR
我有一個 「撰寫評論」 鏈接,如下所示:
<%= link_to 'Write', new_review_path(@new, :vendor_id => @vendor.id) %>
這將創建網址:
評論/新VENDOR_ID = 10
所有我想要的是創造基於三個輸入的新評論對象:
- vendor_id(above)
- USER_ID(這是工作目前)從形式
- @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| %>
......
我可以幫忙嗎?謝謝!
這裏是新的視圖代碼(注:我還創建了一個的has_many路由,這樣的供應商/ 1 /評論/新有意義的,雖然我不調用它了):
我在航線代碼。 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>
嗨 - 感謝 - 我需要進行更改創造呢?我一定已經做了一些調整它的東西,它不工作....謝謝 – AFG 2009-04-19 02:28:39