这是我的表格:
<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 %>
我的部分:
<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>
这是我定义变量的控制器:
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
我想要发生的是在点击“更新”按钮后,我希望从下拉列表中选择的新国家/地区被放入HHActiveCarrier模型中。有了我现在的代码,我收到了这个错误:
OCIError:ORA-00904:“ID”:无效标识符:UPDATE hh_active_carriers设置名称='AT&amp; T',country = null WHERE id = null
我将如何更新属性?我在rails 2.3.8上使用ruby。
编辑:
从开发日志添加参数哈希:
参数:{“commit”=&gt;“更新”,“运营商”=&gt; {“country”=&gt;“USA”}, “action”=&gt;“update”,“controller”=&gt;“active_carriers”}
content_type:#
接受:[#,#,#]
raw_post: “载体%5Bcountry%5D =美国及其载体%5Bcountry%5D =美国及其载体%5Bcountry%5D =美国及其载体%5Bcountry%5D =美国及其提交=更新”
query_parameters:{}
request_parameters:{“commit”=&gt;“更新”,“操作”=&gt;“更新”, “carrier”=&gt; {“country”=&gt;“USA”},“controller”=&gt;“active_carriers”}
EDIT3:
表格:
<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 %>
部分:
<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>
控制器:
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
答案 0 :(得分:2)
一些事情:
fields_for
helper(向下滚动近1/2,对于标有“或要使用的集合:”的代码段)因此,您希望从表单中获取的哈希应该类似于:
params => {:carrier[1] => {:country => "USA", :id=>"5"}, carrier[2] => {:country => "Brazil", :id=>"17"}}
然后在您的控制器中,您将遍历params[:carrier].each
以更新您的运营商。