[英]Ruby on Rails: Updating data after form submission
This is my form: 这是我的表格:
<table>
<tbody>
<% form_for :HhActiveCarrier, @carriers, :url => { :action => "update" } do |f| %>
<% for carrier in @carriers %>
<tr>
<%= render :partial => "summary_detail", :locals => {:carrier => carrier, :f => f} %>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Update" %>
<% end %>
With my partial: 我的部分:
<td class="tn"><%= h(carrier.name.to_s()) -%></td>
<td class="sc"><%= h(carrier.country.to_s()) -%></td>
<td class="sc"><%= select(:carrier, "country", @countries) -%></td>
This is the controller where I define the variables: 这是我定义变量的控制器:
class ActiveCarriersController < ApplicationController
def index
@carriers = HhActiveCarrier.find(:all)
for carrier in @carriers
country = carrier["country"]
if country.nil?
carrier["country"] = "none"
end
end
@countries = ["USA", "UK", "Canada"]
end
def update
carriers = HhActiveCarrier.find(:all)
for carrier in carriers
carrier.update_attributes(params[:country])
end
redirect_to( :action => "index" )
end
What I want to happen is after I click the "Update" button, I want the new country selected from the drop down list to be put into the HHActiveCarrier model. 我想要发生的是在点击“更新”按钮后,我希望从下拉列表中选择的新国家/地区被放入HHActiveCarrier模型中。 With the code I have right now, I get this error:
使用我现在的代码,我收到此错误:
OCIError: ORA-00904: "ID": invalid identifier: UPDATE hh_active_carriers SET name = 'AT&T', country = null WHERE id = null
OCIError:ORA-00904:“ID”:无效标识符:UPDATE hh_active_carriers SET name ='AT&T',country = null WHERE id = null
How would I go about updating the attributes this? 我该如何更新属性呢? I am using ruby on rails 2.3.8.
我在rails 2.3.8上使用ruby。
Edit: 编辑:
Added parameters hash from development log: 从开发日志添加参数哈希:
parameters: {"commit"=>"Update", "carrier"=>{"country"=>"USA"}, "action"=>"update", "controller"=>"active_carriers"}
参数:{“commit”=>“Update”,“carrier”=> {“country”=>“USA”},“action”=>“update”,“controller”=>“active_carriers”}
content_type: #
内容类型: #
accepts: [#, #, #]
接受:[#,#,#]
raw_post: "carrier%5Bcountry%5D=USA&carrier%5Bcountry%5D=USA&carrier%5Bcountry%5D=USA&carrier%5Bcountry%5D=USA&commit=Update"
raw_post:“carrier%5Bcountry%5D = USA&carrier%5Bcountry%5D = USA&carrier%5Bcountry%5D = USA&carrier%5Bcountry%5D = USA&commit = Update”
query_parameters: {}
query_parameters:{}
request_parameters: {"commit"=>"Update", "action"=>"update", "carrier"=>{"country"=>"USA"}, "controller"=>"active_carriers"}
request_parameters:{“commit”=>“Update”,“action”=>“update”,“carrier”=> {“country”=>“USA”},“controller”=>“active_carriers”}
Edit3: EDIT3:
Form: 形成:
<table>
<tbody>
<% form_for :HhActiveCarrier, :url => { :action => "update" } do |f| %>
<% for carrier in @carriers %>
<tr>
<%= render :partial => "layouts/summary_detail", :locals => {:carrier => carrier, :f => f} %>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Update" %>
<% end %>
Partial: 部分:
<td class="tn"><%= h(carrier.name.to_s()) %></td>
<td class="sc"><%= h(carrier.id.to_s()) %></td>
<td class="sc"><%= h(carrier.country.to_s()) %></td>
<td class="sc"><%= f.collection_select :country, HhActiveCarrier::COUNTRIES, :to_s, :to_s %></td>
Controller: 控制器:
class ActiveCarriersController < ApplicationController
def index
@carriers = HhActiveCarrier.find(:all)
for carrier in @carriers
country = carrier["country"]
if country.nil?
carrier["country"] = "none"
end
end
end
def update
#update code
redirect_to( :action => "index" )
end
end
A few things: 一些东西:
fields_for
helper for each of the carriers (scroll down almost 1/2 way, for the code snippet that's labeled "Or a collection to be used:") fields_for
帮助器(向下滚动近1/2,对于标记为“或要使用的集合:”的代码片段) So, the hash you want from the form should look something like: 因此,您希望从表单中获取的哈希应该类似于:
params => {:carrier[1] => {:country => "USA", :id=>"5"}, carrier[2] => {:country => "Brazil", :id=>"17"}}
Then in your controller, you would loop through params[:carrier].each
to update your carriers. 然后在您的控制器中,您将遍历
params[:carrier].each
来更新您的运营商。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.