Reputation: 33
I am stuck at passing a variable value from controller to partial. I have all the data from skill set database on @skillSetData coming from controller: SkillSetsController
def skillsetdata
@skillSetData = SkillSet.all
end
The code below on skill_set/skillsetdata.html.erb display all the data.
<% @skillSetData.each do |single_skill| %>
<option value="<%= single_skill.skillname %>"><%=single_skill.skillname %></option>
<% end %>
But I am not able to pass the variable if skillsetdata.html.erb is a partial ie: _skillsetdata.html.erb. The code for the _skillsetdata I have written is:
<% skills_set_data.each do |single_skill| %>
<option value="<%= single_skill.skillname %>">
<%=single_skill.skillname %>
</option>
<% end %>
I want to make it partial because it has the dropdown list that has to be rendered on various other pages. One of the pages that I want to use on is:static_pages/home.html.erb which is from different controller ie: staticPages. The code that I have tried after the help is:
<%= render partial: 'skill_sets/skillsetdata', locals: { skills_set_data:@skillSetData } %>
But I am getting error as: undefined method `each' for nil:NilClass in skill_sets/_skillsetdata.html.erb while trying to load home.html.erb. I am not able to pass data between different pages from a partial.
Any help with example code would be helpful.
Upvotes: 3
Views: 6750
Reputation: 2973
I guess that you problem here: skill_sets/skillsetdata
<%= render partial: 'skill_sets/skillsetdata', locals: { skills_set_data:@skillSetData } %>
As you wrote that:
The code below on skill_set/skillsetdata.html.erb display all the data.
That the right name is: skill_set/skillsetdata
Here is right name:
<%= render partial: 'skill_set/skillsetdata', locals: { skills_set_data:@skillSetData } %>
Upvotes: 0
Reputation: 33420
For passing instance variables to partials you can use the locals
:
# view from
<%= render partial: 'skillsetdata', locals: { skills_set_data: @skillSetData } %>
While in the local you receive the value of that instance variable, which is now a local one:
# partial
<% skills_set_data.each do |single_skill| %>
<option value="<%= single_skill.skillname %>">
<%=single_skill.skillname %>
</option>
<% end %>
Upvotes: 3