Rails - 未定义方法“”for - 具有选择倍数的嵌套表单

时间:2016-01-30 13:49:35

标签: ruby-on-rails ruby ruby-on-rails-4

我无法找到如何在我的dealsController中修复我的“未定义方法”银行“for”。

我的模特:

<input type="hidden" name="SelectedAnswers.Index" value="@Model.Key">

我创建交易的嵌套表格:

class Deal < ActiveRecord::Base
    has_many :pools
    accepts_nested_attributes_for :pools, 
                        reject_if: proc { |attributes| attributes['name'].blank?},
                        allow_destroy: true
    validates :name, presence: true
end

class Pool < ActiveRecord::Base
    belongs_to :deal
    has_many :participating_banks, dependent: :destroy
    has_many :banks, through: :participating_banks

    validates :name, presence: true
end
class Bank < ActiveRecord::Base
    has_many :participating_banks, dependent: :destroy
    has_many :pools, through: :participating_banks
end

class ParticipatingBank < ActiveRecord::Base
  belongs_to :pool
  belongs_to :bank
end

我的部分pool_pools:

<%= form_for @deal do |f| %>
<p><%= f.text_field :name, placeholder: "Deal name" %></p>
<p><%= f.date_field :closing_date, placeholder: "Closing date" %></p>

<h2> Pools </h2>
<%= f.fields_for :pools do |builder| %>
<%= render 'pool_pools', f: builder %>
<% end %>
<%= link_to_add_pools "Add pool", f, :pools %>

<p><%= f.submit %></p>
<% end %>

我的Deal / Show视图触发了问题(@ banks.each):

<fieldset>
<%= f.label :name %> <br/>
<%= f.text_field :name %>
<%= f.hidden_field :_destroy %> <br/>
<%= f.select :bank_ids, Bank.all.collect {|x| [x.name, x.id]}, {}, :multiple => true %> <br/>
<%= link_to "remove", "#", class: "remove_pools" %>
</fieldset>

如何定义我的show action我的DealsController只显示属于池的银行(属于交易)?

<h1> <%= @deal.name %> </h1>
<p> <%= @deal.closing_date %> </p>

<% if @deal.pools.any? %>
    <ul>
        <% @pools.each do |pool| %>
        <li> <%= pool.name %> </li>
            <ul>
                <% @banks.each do |bank| %>
                <%= bank.name %>
                <% end %>
            </ul>
        <% end %>
    </ul>
<% else %>
    <p> No pools created yet - <%= link_to "New Pool", new_pool_path %> </p>
<% end %>

<%= link_to "Home", root_path %>
<%= link_to "Edit deal", edit_deal_path(@deal) %>
<%= link_to "Delete deal", deal_path(@deal), method: :delete, data: {confirm: "Are you sure?"} %>

编辑:

以下是更新操作的日志:

def show
        @pools = @deal.pools
        @banks = @pool.banks
    end

他是完整的错误消息:

Started PATCH "/deals/9" for ::1 at 2016-01-30 15:11:22 +0100
Processing by DealsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2G45qGM1BbfFKI38O6h+UkEdlKQNNIG5HkTy3llnO4Nq/xSIOzBr+TQhntEh90QXWHh6R2n0HG5uCxtCiU8yZg==", "deal"=>{"name"=>"Deal test 1é", "closing_date"=>"", "pools_attributes"=>{"0"=>{"name"=>"pool 10001", "_destroy"=>"false", "bank_ids"=>["", "1", "2"], "id"=>"12"}, "1"=>{"name"=>"", "_destroy"=>"false", "bank_ids"=>[""]}}}, "commit"=>"Update Deal", "id"=>"9"}
  [1m[36mDeal Load (0.2ms)[0m  [1mSELECT  "deals".* FROM "deals" WHERE "deals"."id" = ? LIMIT 1[0m  [["id", 9]]
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36mPool Load (0.4ms)[0m  [1mSELECT "pools".* FROM "pools" WHERE "pools"."deal_id" = ? AND "pools"."id" = 12[0m  [["deal_id", 9]]
  [1m[35mBank Load (0.2ms)[0m  SELECT "banks".* FROM "banks" WHERE "banks"."id" IN (1, 2)
  [1m[36mBank Load (0.2ms)[0m  [1mSELECT "banks".* FROM "banks" INNER JOIN "participating_banks" ON "banks"."id" = "participating_banks"."bank_id" WHERE "participating_banks"."pool_id" = ?[0m  [["pool_id", 12]]
  [1m[35m (0.1ms)[0m  commit transaction
Redirected to http://localhost:3000/deals/9
Completed 302 Found in 28ms (ActiveRecord: 1.6ms)


Started GET "/deals/9" for ::1 at 2016-01-30 15:11:22 +0100
Processing by DealsController#show as HTML
  Parameters: {"id"=>"9"}
  [1m[36mDeal Load (0.1ms)[0m  [1mSELECT  "deals".* FROM "deals" WHERE "deals"."id" = ? LIMIT 1[0m  [["id", 9]]
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.1ms)

NoMethodError (undefined method `banks' for nil:NilClass):
  app/controllers/deals_controller.rb:10:in `show'

非常感谢:)

1 个答案:

答案 0 :(得分:0)

只需在视图的迭代中使用pool.banks。您的控制器是错误的,因为您试图找到多个池的存储区。

      <% @pools.each do |pool| %>
        <li><%= pool.name %> </li>
        <ul>
            <% pool.banks.each do |bank| %>
              <%= bank.name %>
            <% end %>
        </ul>
      <% end %>