Ruby on rails RubyonRails:表单提交后更新数据
这是我的表格:Ruby on rails RubyonRails:表单提交后更新数据,ruby-on-rails,Ruby On Rails,这是我的表格: <table> <tbody> <% form_for :HhActiveCarrier, @carriers, :url => { :action => "update" } do |f| %> <% for carrier in @carriers %> <tr> <%= render :partial => "summary_detail", :locals
<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 %>
{:action=>“update”}do | f |%>
“摘要详细信息”,:locals=>{:carrier=>carrier,:f=>f}%>
用我的部分:
<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
class ActiveCarrierController“index”)
结束
我希望在单击“更新”按钮后,将从下拉列表中选择的新国家/地区放入HHActiveCarrier模型中。使用我现在拥有的代码,我得到以下错误:
错误:ORA-00904:“ID”:无效标识符:更新
hh\u活动\u运营商集名称='AT&T',国家=空,其中id=空
我将如何更新此属性?我正在使用RubyonRails 2.3.8
编辑:添加了开发日志中的参数哈希: 参数:{“提交”=>“更新”,“承运人”=>{“国家”=>“美国”}, “操作”=>“更新”,“控制器”=>“活动载体”} 内容类型:# 接受:[#,#,#] 原始职位: “承运人%5B国家%5D=美国和承运人%5B国家%5D=美国和承运人%5B国家%5D=美国和承运人%5B国家%5D=美国和提交=更新” 查询参数:{} 请求参数:{“提交”=>“更新”,“操作”=>“更新”, “承运人”=>{“国家”=>“美国”},“控制员”=>“有效承运人”} 编辑3: 表格:
<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 %>
{:action=>“update”}do | f |%>
“布局/摘要\详细信息”,:locals=>{:carrier=>carrier,:f=>f}%>
部分:
<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
class ActiveCarrierController“index”)
结束
结束
有几件事:
params => {:carrier[1] => {:country => "USA", :id=>"5"}, carrier[2] => {:country => "Brazil", :id=>"17"}}
然后在控制器中,您将循环通过
params[:carrier]。每个更新您的载体。您可以在提交时从开发日志中发布params散列吗?从我阅读代码的方式来看,您似乎选择了所选的国家,并将其设置为每个运营商的国家,而不是将正确的国家和运营商配对在一起。如果看不到实际提交的内容,很难判断。谢谢我做了编辑。这就是你要找的吗?是的,我想这就是我要找的。试着去掉
中的=
来解决关于意外的第一个错误。
或者可能将=
添加到“我永远记不起哪个是正确的,但我总是犯那个错误。我用我尝试过的方法进行了编辑。任何帮助都将不胜感激。谢谢我又做了几次编辑。我仍然不确定需要做什么才能放入ActiveCarrierController.update以更新我的HhActiveCarrier模型和数据库。谢谢