kdweber89
kdweber89

Reputation: 2174

undefined method `model_name' for nil:NilClass within simple form with two partials

I have two partials that route to the same controller and same model. In both of these partials I am submitting a new form, which I am intending on using the create method in my controller. My first partial is simply called _form.html.haml and it works quite well. However my second partial I have called _case_study_form.html.haml. My second partial is where I am having some problems. The issue that I am getting is undefined method 'model_name' for nil:NilClass. I believe that I am getting this issue because my second partial has a different name, I don't understand why I am getting this error as I feel like I have routed the model in the correctly.

My model is named form_submission.rb

My first partial, _form.html.haml I have my first line set up as

= simple_form_for @form_submission do |f|

My second partial, _case_study_form.html.haml I am having troubles with my first line and have it set up as

= simple_form_for @form_submission, url: case_study_path do |f|

My controller is

class FormSubmissionsController < ApplicationController
  invisible_captcha only: [:create, :case_study], on_spam: :handle_spam

  def new
    @form_submission ||= FormSubmission.new
  end

  def create
    @form_submission = FormSubmission.new(form_submission_params)

    if @form_submission.save
      redirect_to thank_you_path
    else
      render :new
    end
  end

  def case_study
  end

  private

    def handle_spam
      redirect_to root_path
    end

    # Only allow a trusted parameter "white list" through.
    def form_submission_params
      params.require(:form_submission).permit(:first_name, :last_name, :organization, :email, :phone, :recognition, :inquiry, :form_submission)
    end
end

Upvotes: 0

Views: 106

Answers (1)

Sebin
Sebin

Reputation: 1044

You need @form_submission object initialised inside the method case_study as well. The url you mention in the form represents the form submit action controller method. I.e., once you submit the form, it will go to that url.

def case_study
  @form_submission ||= FormSubmission.new
end

Upvotes: 1

Related Questions