3

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.

4
  • ActionController::UnknownFormat is a different error. What's the code in your controllers? Commented Dec 6, 2017 at 3:37
  • I realized the error was actually undefined method `each' for nil:NilClass in skill_sets/_skillsetdata.html.erb while trying to load home.html.erb Commented Dec 6, 2017 at 3:54
  • There's only one reason to get that error. The instance variable you're using is nil. Otherwise if the partial doesn't exist, would be Missing partial, if the local variable setted doesn't match in name in the partial then undefined local variable or method if @skillSetData is nil then undefined method 'each' for nil:NilClass. Give it a try, use locals: { skills_set_data: nil } and see what happens. Commented Dec 6, 2017 at 4:16
  • 1
    You were correct at the first. The only missing part on the code was to define @skillSetData = SkillSet.all on the controller of home.html.erb. instead of trying to get it from SkillSetData controller. As per my research I found that the variable of the controller cannot be passed to the view of anonther controller. But we can grab the data of different model to the controller of different model. Thank you for helping out. Commented Dec 6, 2017 at 4:45

2 Answers 2

3

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 %>
Sign up to request clarification or add additional context in comments.

5 Comments

I am trying to render the partial from a view static_pages/home.html.erb of different controller ie: staticPages#home. So when I have <%= render partial: 'skill_sets/skillsetdata', locals: { skills_set_data: @skillSetData } %> on home.html.erb . On loading the view it gives me undefined method `each' for nil:NilClass error in the partial ie: skill_sets/_skillsetdata.html.erb. How does the variable data passes to the partial?
It depends on what you're accessing and using each. @skillSetData is your instance variable defined in the controller, skills_set_data is the variable that'll be available in the partial. What's the code giving you the error?, can you add it?
<% skills_set_data.each do |single_skill| %> . This part of the code in partial ie:skill_sets/_skillsetdata.html.erb is giving the error. The page I am loading is static_pages/home.html.
Can you add both files involved in the problem, to your question?
I have updated my answer with codes on all the files. How do I get data from Skillsets to the view of another controller using partial?
0

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 } %>

1 Comment

The file is under the folder skill_sets so I believe correct would be skill_sets/skillsetdata

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.