Rails 4 中模型的未定义方法“each_with_index”

undefined method `each_with_index' for model in Rails 4

我有以下型号:

class Account
  has_many :account_configs
  accepts_nested_attributes_for :account_configs
end

class AccountConfig
  belongs_to :account
end

控制器

def show
end


def new
  @account = Account.new
  @account_config = @account.account_configs.build
end

def create
  @account = Account.new(account_params)
  if @account.save
    redirect_to account_path(@account), notice: 'Account was successfully created.'
  else
    render :new
  end
end

def account_params
      params.require(:account).permit(:name, account_configs_attributes: [:id, :type, :duration, :branch]) if params[:account]
end

在以嵌套形式创建模型 Account 及其配置后,我应该在 show 页面中显示它。我需要遍历此 ActiveRecord_Associations_CollectionProxy - @account.account_configs。还有一个之前写好的layout_builder文件。我还需要为此页面扩展相同的布局。当我尝试像这样循环时

@account.account_configs.each do |config|
  #here extending from the layout which has other check methods
end

我收到此错误:undefined method each_with_index for #<AccountConfig:0----->

但是如果我在上面使用 where 而不是 each,布局会被扩展和渲染。我可以使用 where 但是重复了很多代码。在这种情况下,我应该怎么做才能使这个错误消失?

error_log

D, [2019-07-24T14:06:35.851681 #25672] DEBUG -- :   Account Load (1.2ms)  SELECT  `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 5 LIMIT 1
D, [2019-07-24T14:06:35.863820 #25672] DEBUG -- :   AccountConfig Load (1.3ms)  SELECT  `account_config`.* FROM `account_config` WHERE `account_config`.`account_id` = 5  ORDER BY `account_config`.`id` ASC LIMIT 1
D, [2019-07-24T14:06:35.874536 #25672] DEBUG -- :   CACHE (0.0ms)  SELECT  `account_config`.* FROM `account_config` WHERE `account_config`.`account_id` = 5  ORDER BY `account_config`.`id` ASC LIMIT 1  [["account_id", 5]]
D, [2019-07-24T14:06:35.887123 #25672] DEBUG -- :   Account Load (1.1ms)  SELECT `accounts`.`id`, `accounts`.`name` FROM `accounts` WHERE `accounts`.`account_id` = 5
D, [2019-07-24T14:06:35.896571 #25672] DEBUG -- :   AccountConfig Load (1.2ms)  SELECT  `account_config`.* FROM `account_config` WHERE `account_config`.`account_id` = 5  ORDER BY `account_config`.`id` ASC LIMIT 1000
I, [2019-07-24T14:06:35.922217 #25672]  INFO -- :   Rendered customer/accounts/show.html.erb within layouts/customer (62.6ms)
I, [2019-07-24T14:06:35.930145 #25672]  INFO -- : Completed 500 Internal Server Error in 101ms (ActiveRecord: 6.9ms)
F, [2019-07-24T14:06:35.960592 #25672] FATAL -- : 
NoMethodError - undefined method `each_with_index' for #<AccountConfig:0x00005581ed35b6e0>:
  lib/layout_builder.rb:693:in `records'
  app/views/customer/accounts/show.html.erb:37:in `block (4 levels) in _app_views_customer_accounts_show_html_erb___136536495548701304_47008262570920'
  lib/layout_builder.rb:641:in `initialize'
  lib/layout_builder.rb:216:in `record_list'
  app/views/customer/accounts/show.html.erb:33:in `block (3 levels) in _app_views_customer_accounts_show_html_erb___136536495548701304_47008262570920'
  app/views/customer/accounts/show.html.erb:31:in `block (2 levels) in _app_views_customer_accounts_show_html_erb___136536495548701304_47008262570920'
  lib/layout_builder.rb:162:in `block in article'
  lib/layout_builder.rb:281:in `level'
  lib/layout_builder.rb:160:in `article'
  app/views/customer/accounts/show.html.erb:2:in `block in _app_views_customer_accounts_show_html_erb___136536495548701304_47008262570920'
  lib/layout_builder.rb:153:in `page'
  app/views/customer/accounts/show.html.erb:1:in `_app_views_customer_accounts_show_html_erb___136536495548701304_47008262570920'

D, [2019-07-24T14:06:36.118610 #25672] DEBUG -- : 
D, [2019-07-24T14:06:36.118734 #25672] DEBUG -- : 
I, [2019-07-24T14:06:36.118873 #25672]  INFO -- : Started POST "/__better_errors/60b69c8a4bd7e26b/variables" for 127.0.0.1 at 2019-07-24 14:06:36 +0530

show.html.erb

<% configs = @account.account_configs %>    

<% configs.each do |config| %>
<div class="custom-class"><h2><%= "#{config.type}" %></h2></div>

  <% a.record_list configs do |rl| %>
    <% rl.header 'duration' %>
    <% rl.header 'branch' %>
    <% rl.records do |data| %>
      <% if data.type == "#{config.type}" %>
        <% rl.show data.duration %>
        <% rl.show data.branch %>
      <% end %>
    <% end %>
  <% end %>
<% end %>

@account.account_configs.each do |config| 将循环为您提供您调用 each_with_index

的每个 AccountConfig 块变量 config 形式的单个对象

each_with_index 仅支持 collection/enumerable 对象而不支持单个对象 AccountConfig.

我在 User 对象上调用该方法时遇到了同样的错误,

NoMethodError: undefined method `each_with_index' for #<User:0x00000007128c00>

检查以下 each_with_index

lib/layout_builder.rb:216:in `record_list'
lib/layout_builder.rb:693:in `records'

解决方案-

您可以通过将对象 config 作为 [config] 传递来解决,它将充当可枚举对象

我们不能对单个对象使用 each_with_index,它适用于活动记录集合。如果您需要当前循环内的 account_configs 个对象的索引。试试这个循环。

@account.account_configs.each_with_index do |config, index|
  # index - will give the current loop count.
end