bugman bugman - 2 years ago 68
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 }


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'
flash[:error] = "Error on creating food"
render '/locals'

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

What's the issue? Thank you

Answer Source

Then change your view code as:

<%= form_for :food, :url => {:action => :create} do |f| %>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download