我有以下型号:
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----->
。
但如果我在同一页面上使用each
,则可以扩展并渲染布局,而不是where
。我可以使用where
,但是要重复很多代码。在这种情况下,我应该怎么做才能使此错误消失?
错误日志
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 %>
答案 0 :(得分:1)
@account.account_configs.each do |config|
将循环以块变量AccountConfig
的形式为您提供每个config
单个对象,您在其上调用了each_with_index
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]
来解决,该对象将充当可枚举的对象
答案 1 :(得分:0)
我们不能在单个对象上使用each_with_index,它可以用于活动记录收集。如果您需要当前循环内的account_configs对象的索引。试试这个循环。
@account.account_configs.each_with_index do |config, index|
# index - will give the current loop count.
end