Search code examples
ruby-on-railssimple-form

simple_form_for undefined method `model_name' for NilClass:Class


I receive the following error when building a basic form to create new data. When I hit submit I get

simple_form_for undefined method `model_name' for NilClass:Class

**_form.html.erb**
<%= simple_form_for(@calls) do |f| %>
  <%= f.error_notification %>
  <%= f.input :caller_name %>
  <%= f.input :caller_phone, hint: "xxx-xxx-xxxx" %>
  <%= f.input :caller_address %>
  <%= f.input :primary_diagnosis %>
  <%= f.error :base %>
  <%= f.button :submit %>
<% end %>

**calls_controller.rb**
def create
     @call = Call.new(params[:call])

     respond_to do |format|
       if @call.save
         format.html { redirect_to @call, notice: 'Call was successfully created.' }
         format.json { render json: @call, status: :created, location: @call }
       else
         format.html { render action: "new" }
         format.json { render json: @call.errors, status: :unprocessable_entity }
       end
     end
   end

**new.html.erb**
<h1>New Call</h1>

<%= render 'form' %>

<%= link_to 'Back', calls_path %>

I'm at a bit of a lost here as I've followed rails naming conventions, even tried this with a scaffold with the same result. Already restarted the webrick as well. Help?


Solution

  • You are asking for a simple_form_for(@calls)

    Yet your controller is creating @call

    You should change

    <%= simple_form_for(@calls) do |f| %>
    

    to

    <%= simple_form_for(@call) do |f| %>