这是我的形式:Ruby on Rails的:更新表单提交后数据
<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": invalid identifier: UPDATE hh_active_carriers SET name = 'AT&T', country = null WHERE id = null
我将如何去更新属性呢?我在rails 2.3.8上使用ruby。
编辑:
添加的参数哈希从开发日志:
parameters: {"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"
query_parameters: {}
request_parameters: {"commit"=>"Update", "action"=>"update", "carrier"=>{"country"=>"USA"}, "controller"=>"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
你可以从你的开发日志中发布params散列吗?我读取代码的方式,看起来像是选择了国家,并将其设置为每个运营商的国家,而不是将正确的国家和运营商组合在一起。没有看到实际提交的内容很难说清楚。谢谢! – jefflunt
我做了一个编辑。那是你在找什么? –
是的,我认为这是我正在寻找的。 – jefflunt