user6218387 user6218387 - 7 months ago 25
Ruby Question

NoMethodError in Cloths#index

I am having trouble solving this problem. I keep getting the same error:


(undefined method `id' for nil:NilClass):


Here my application.html

<% @categories.each do |category| %>
<li><a><%= link_to 'category.title', category_cloth_path(@category)%></a></li>
<%end%>


cloths_controller.rb

def index
@cloths = Cloth.all
@categories = Category.all
end

def show
@cloths = Cloth.all
@categories = Category.where("category_id = ?", @category.id)
@comments = Comment.where("cloth_id = ?", @cloth.id)
@comments = Comment.paginate(:page => params[:page], :per_page => 3)
end

Answer

There must be a problem with your Routes.erb if the problem (undefined method `id' for nil:NilClass): exists that means your category_cloth_path(@category) is not defined in routes... try putting in routes.erb

Rails.application.routes.draw do    
resources :cloth
     member do
      get 'category'
     end
    end
end
Comments