undefined method 'model_name' for NilClass:Class

17,211

Solution 1

Then change your view code as:

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

Solution 2

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

Share:
17,211
bugman
Author by

bugman

Updated on June 04, 2022

Comments

  • bugman
    bugman almost 2 years

    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

    • Trip
      Trip almost 12 years
      What is @local ? That sounds undefined.
    • bugman
      bugman almost 12 years
      @local = Local.find(params[:id])
    • bugman
      bugman almost 12 years
      you mean the scope of that variable is wrong?
    • Casper
      Casper almost 12 years
      You have @food in one controller action and @foods in another, yet the form is for @food (singular), and you say the error is for Locals#show where you use @foods (plural). Something doesn't match up there in your code.
    • bugman
      bugman almost 12 years
      i have @foods because i want to show in show.html.erb also a list of foods but the form is for a singular food so i don't understand
    • bugman
      bugman almost 12 years
      in form i have @ food (singular for one food) in show i have @foods in order to display a list of foods that belongs to a local (in italian a synonim of "bar"). The <%= form_for :food, :url => {:action => :create} do |f| %> solution seems to be working but raise another issue when i try to create a new food by filling fields and click on submit button i've been redirected to new Local path and no food has been created
  • bugman
    bugman almost 12 years
    wait i have 2 model one is Local (that has_many :foods) and the other is Food (that belongs_to :local) so you mean @food = Food.new ? However unfortunately it doesn't work
  • bugman
    bugman almost 12 years
    I've no more the error when page is displayed but when i try to create a new food by filling fields and click on submit button i've been redirected to new Local path and no food has been created, do you know why?
  • bugman
    bugman almost 12 years
    when i try to post the debugger writes the following : food: !ruby/hash:ActiveSupport::HashWithIndifferentAccess nome: a descrizione: '' prezzo: '10' commit: Post action: create controller: locals ...How can i change the controller from locals to foods ?
  • Salil
    Salil almost 12 years
    You need to add this code in your 'show' action. Did you understand that your '@food' instance variable is not set in show action and that is why you are getting this error?