bugman bugman - 7 months ago 15
Ruby Question

undefined method 'model_name' for NilClass:Class

the browser returned me the following error in Locals#show:

undefined method 'model_name' for NilClass:Class

51: <%= form_for(@food) do |f| %>
52: <%= render 'shared/error_messages', object: f.object %>
53: <div class="field">
54: <%= f.label :nome %>


Here is my locals_controller.rb (the show action)

def show
@local = Local.find(params[:id])
@foods = @local.foods.paginate(page: params[:page])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @local }
end


end

And here foods_controller.rb (the create action)

def create
@food = @local.foods.build(params[:food])
if @food.save
flash[:success] = "Food created!"
redirect_to '/locals'
else
flash[:error] = "Error on creating food"
render '/locals'
end
end


Food model and Local model are related with :has_many and belongs_to

What's the issue? Thank you

Answer

Then change your view code as:

<%= form_for :food, :url => {:action => :create} do |f| %>