Rails - why I'm getting undefined method `name' for nil:NilClass



I'm displaying question by categories and each question has its own number. I made it like this(index.html.erb):

<% @categories.each do |category| %>
     <div id=<%= "box#{category.id}" -%>>
        <h1><%= category.name %></h1>
        <% category.questions.each_with_index do |question, i|
%>
            <ul class="question-list" style="display: inline;">
                <li><%= link_to (i + 1), show_path
%></li>
            </ul>
        <% end %>
     </div>
<% end %>

And when I click on question number I would like to show it like this(show.html.erb):

<div class="question-wrapper">
       <h1><%= @category.name %></h1>
       <div class="image left-align">
         <%= question_image_tag(question, :large) %>
       </div>
       <div class="question-text right-align">
        <%= @question.question_name %>
       </div>
       <p><%= @question.answer %></p>
 </div>

This is my controller:

def index
    @categories = Category.all
    @questions = Question.all
end

def show
    @categories = Category.all
    @questions = Question.all
end

Now, I'm getting undefined method `name' for nil:NilClass (also throws error for all methods in show.html.erb) and I can't resolve it.

Question: What should I do to resolve undefined method `name' for nil:NilClass?


Related to : Rails - why I'm getting undefined method `name' for nil:NilClass

Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .