我之前曾问过这个相关问题,但没有找到任何帮助Rails 4 - how to use a helper method in an index view
我在这方面有同样的问题。必须有一些东西阻止我在索引操作中使用辅助方法。
我有索引视图和显示视图。帮助程序在show视图中正常工作。
在我的索引视图中,我有:
<% @eois.sort_by(&:created_at).each do |eoi| %>
<div class="col-md-3 col-sm-5 portfolioitem Scienza">
<div class="portfolio-item text-center">
<h4><%= link_to eoi.user.full_name %></h4>
<span>Interested<%= display_interest(eoi) %></span>
<%= link_to 'VIEW DETAILS', project_eoi_path(@project, eoi), :class=> "portfolio-item-view" %>
</div>
</div>
<% end %>
在我的节目视图中,我有:
<td><%= @eoi.user.full_name %></td>
<td><%= @eoi.user.profile.organisation.try(:title) %></td>
<td>Interested<%= display_interest(@eoi) %></td>
<td><%= @eoi.created_at.try(:strftime, '%e %B %Y') %></td>
在我的帮助文件中,我有:
def display_interest(eoi)
if interested_in_contributing
'in contributing resources to this project team'
elsif interested_in_participating
'in participating in this project'
elsif interested_in_partnering
'in partnering with this project team'
elsif interested_in_granting
'in assessing this project for a grant'
elsif interested_in_investing
# elsif eoi.invest || @invest
'in investing in the outcomes of this project'
else
nil
end
end
# Depending on the type of interest, then figure out which questions need to be asked.
# If the interest is in participation - there is no need to ask questions relating to asset requests
# TODO - need a better way to check the js on participant_intrest to feed through this same channel
def interested_in_participating
@eoi.participate || @participate
end
# If the interest is in contributing assets or partnering, need to check which assets are relevant to the interest
def interested_in_contributing
@eoi.contribute || @contribute
end
为什么我不能使用索引视图中的帮助文件?
控制器:
def index
@eois = Project.by_user_id(current_user.id).find_by(id: params[:project_id]).try(:eois) || Eoi.none
policy_scope(@eois)
end
def show
@eoi = Eoi.find(params[:id])
authorize @eoi
end
答案 0 :(得分:0)
试试这个
def display_interest(eoi)
if interested_in_contributing eoi
'in contributing resources to this project team'
.
.
def interested_in_contributing eoi
eoi.contribute || @contribute
end
答案 1 :(得分:0)
错误显示undefined method contribute on nil class
并且正在@eoi
上调用此方法,检查您是否在索引操作上定义了@eoi
实例变量。它应该看起来像:
def index
@eoi = 'some value'
end
可能已在您的show
操作中定义,但未在index
操作中定义。希望它有所帮助。
答案 2 :(得分:0)
您应该将变量eoi
作为参数传递给interested_in_participating
和interested_in_contributing
方法。这是因为对于索引操作,实例变量是@eois
,而不是@eoi
。这是代码段。
def interested_in_participating(eoi)
eoi.participate || @participate
end
# If the interest is in contributing assets or partnering, need to check which assets are relevant to the interest
def interested_in_contributing(eoi)
eoi.contribute || @contribute
end
# Add parameter while calling the methods
def display_interest(eoi)
if interested_in_contributing(eoi)
'in contributing resources to this project team'
elsif interested_in_participating(eoi)
'in participating in this project'
elsif interested_in_partnering
'in partnering with this project team'
elsif interested_in_granting
'in assessing this project for a grant'
elsif interested_in_investing
# elsif eoi.invest || @invest
'in investing in the outcomes of this project'
else
nil
end
end
如果他们使用@eoi
实例,那么其他方法也是如此。