Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 361
bugman Avatar asked Dec 27 '22 21:12

bugman


1 Answers

Check by adding following code (I assumed that your model is Local)
@food = Local.new
In your new action

like image 101
Ganesh Kunwar Avatar answered Jan 13 '23 12:01

Ganesh Kunwar