Ich habe zuvor diese verwandte Frage gestellt, aber keine Hilfe gefunden Rails 4 - how to use a helper method in an index viewRails 4 - Wie kann ich eine Hilfsmethode in einer Indexansicht verwenden?
Ich habe das gleiche Problem in diesem Zusammenhang. Es muss mich etwas daran hindern, Hilfsmethoden in meiner Indexaktion zu verwenden.
Ich habe eine Indexansicht und eine Showansicht. Der Helfer funktioniert gut in der Showansicht.
In meiner Indexansicht ich habe:
<% @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 %>
In meiner Show Ansicht, die ich habe:
<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>
In meiner Helfer-Datei, die ich habe:
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
Wie kommt Ich kann die Hilfsdatei nicht aus der Indexansicht verwenden?
Controller:
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
"Es muss mich etwas davon abhalten, Hilfsmethoden in meiner Indexaktion zu verwenden" - Das ist seltsam, was der Fehler ist, den du bekommst ?? –
undefined Methode 'contribute 'für nil: NilClass – Mel
Sie sollten Argumente an Ihre Helfer weitergeben, ohne sich auf Instanzvariablen zu verlassen. –