2011-09-23 57 views
0

这是我的形式: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 
+0

你可以从你的开发日志中发布params散列吗?我读取代码的方式,看起来像是选择了国家,并将其设置为每个运营商的国家,而不是将正确的国家和运营商组合在一起。没有看到实际提交的内容很难说清楚。谢谢! – jefflunt

+0

我做了一个编辑。那是你在找什么? –

+0

是的,我认为这是我正在寻找的。 – jefflunt

回答

2

有几件事情:

  1. 调整你的表格,以便它使用the fields_for helper每个载波的(向下滚动大约一半的方式,对代码段的标签名为“还是要使用的集合:“)
  2. 在您的partial中添加一个隐藏字段,用于指示要更新的载体的ID(现在,您的params散列不包含要更新的记录的ID,因此更新失败)
  3. 不要遍历所有您的控制器中的载体。你想通过散列来代替。

因此,从形式你想要的哈希应该是这个样子:

params => {:carrier[1] => {:country => "USA", :id=>"5"}, carrier[2] => {:country => "Brazil", :id=>"17"}} 

然后在你的控制器,你会遍历params[:carrier].each更新您的运营商。

+0

我用我试过的东西做了编辑。任何帮助,将不胜感激。谢谢! –

+0

我做了几个更多的编辑。我仍然不确定我需要做什么来放置ActiveCarriersController.update来更新我的HhActiveCarrier模型和数据库。谢谢。 –

+0

我最终得到了这个工作。谢谢! –

相关问题