Reputation: 1160
I am trying to render a partial of a collection using a name different to that of the model:
<%= render "current_campaigns", collection: @current_campaigns, as: :current_campaign %>
The model is called Campaign but this is a subset of campaigns as defined in the controller action:
def index
@current_campaigns = Campaign.where(status: :approved)
end
In the partial (which lives in the application directory not the campaigns directory):
<%= current_campaign.question %>
The resulting error:
undefined local variable or method `current_campaign' for #<#<Class:0x007fad3d5e5500>:0x007fad451976a8>
I was under the impression that as
would make this work but apparently not. Any thoughts?
Upvotes: 0
Views: 1889
Reputation: 156
Try the following
<%= render partial: 'path-relative-to-views/current_campaigns', collection: @current_campaigns, as: :current_campaign %>
And the partial should name _current_campaign.html.erb
for convention.
Upvotes: 4
Reputation: 4420
Try to rename campaigns/current_campaigns.html.erb template to campaigns/campaign.html.erb and render like that from campaigns/:
<%= render @current_campaigns %>
Otherwise you can specify name of directory where the partial is:
<%= render "other_folder_name/campaign", collection: @current_campaigns %>
Upvotes: 0
Reputation: 411
The most simple way to pass attributes to the partial using a custom name is:
<%= render "current_campaigns", custom_name: @current_campaigns %>
doing this you will be able to use custom_name as a defined variable on your partial.
Upvotes: 0