Rails嵌套表单,带有单选按钮

时间:2017-09-19 20:06:55

标签: ruby-on-rails forms nested

我有三个相关的模型

    class Opportunity < ActiveRecord::Base
    has_many :proposals,  :dependent => :destroy
    end

    class Proposal < ActiveRecord::Base
    belongs_to :opportunity
    belongs_to :panel
    end

    class Panel < ActiveRecord::Base
    belongs_to :opportunity
    has_many :proposals
    accepts_nested_attributes_for :proposals
    end

我只有一次机会,我希望在表单中列出所有提案,每个提案都有四个单选按钮,可以将每个提案分配给四个面板中的一个。

    <%= form_for :proposal, :url => update_all_path, :html => { :method => :put } do %>
     <% @proposals.each do |prop| %>
      <%= fields_for "proposal[]", proposal do |proposal_fields| %>
       <%= proposal.name %><br>
        <%= proposal_fields.number_field :panel_id %><br>
      <% end %>
     <% end %>
    <% end %>

此表单似乎在基本信息中正常工作。理想情况下,我想要一系列单选按钮而不是number_field或select。 所以我会: 提案A - &gt; 4个单选按钮** 提案B - &gt; 4个单选按钮**

现在,我很高兴只需将新的panel_id正确地放入数据库。

这是返回的参数

    "proposal"=>{"244"=>{"panel_id"=>"34"}, "245"=>{"panel_id"=>"33"}}, 

和我的更新方法

    def update_all
      params['proposal'].keys.each do |id|
        @proposal = Proposal.find(id.to_i)
        @proposal.panel_id = params['proposal'][:id][panel_id]
      end
      redirect_to...
    end

并且没有进一步的错误......错误

    NameError at /proposals/all 
    undefined local variable or method `panel_id' for #
    <ProposalsController:0x007ff3cf714420>
    Did you mean?  panel_url

如何为每个提案保存panel_id?

提前感谢您的帮助。

0 个答案:

没有答案