title: Contact form data in Rails 5 form
body: <p>I'm having one of those moments. My problem is probably simple but I'm not getting it figured out. </p>
  
  <p>I have a Rails 5 app that I'm developing. It contains a contact form for the contact to fill out and submit. </p>
  
  <p>Next I have ActionMailer set to send an email to the Contact informing them that their message was received and is being reviewed. I also have ActionMailer set to send an email to the website admin that is supposed to contain the contact form data. This is where I am running into a problem. How do I pass the contact form data into my email view?</p>
  
  <p>Here is the contacts_controller:</p>
  
  <pre><code>class ContactsController &lt; ApplicationController
    before_action :set_contact, only: [:show, :edit, :update, :destroy]
  
    # GET /contacts
    # GET /contacts.
  
    def index
      @contacts = Contact.all
    end
  
    # GET /contacts/1
    # GET /contacts/1.json
    def show
    end
  
  
  
    # GET /contacts/new
    def new
      @contact = Contact.new
    end
  
    # GET /contacts/1/edit
    def edit
    end
  
    # POST /contacts
    # POST /contacts.json
    def create
      @contact = Contact.new(contact_params)
  
      respond_to do |format|
        if @contact.save
  
  
          FormMailer.message_received(@contact).deliver_now 
          FormMailer.new_web_message(@email).deliver_now
          format.html { redirect_to thank_you_path, notice: 'Your message was sent.' }
          format.json { render :show, status: :created, location: @contact }
        else
          format.html { render :new }
          format.json { render json: @contact.errors, status: :unprocessable_entity }
        end
      end
    end
  
  
  
    # PATCH/PUT /contacts/1
    # PATCH/PUT /contacts/1.json
    def update
      respond_to do |format|
        if @contact.update(contact_params)
          format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
          format.json { render :show, status: :ok, location: @contact }
        else
          format.html { render :edit }
          format.json { render json: @contact.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /contacts/1
    # DELETE /contacts/1.json
    def destroy
      @contact.destroy
      respond_to do |format|
        format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_contact
        @contact = Contact.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def contact_params
        params.require(:contact).permit(:first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message)
      end
  end
  </code></pre>
  
  <p>The Mailer:</p>
  
  <pre><code>    class FormMailer &lt; ApplicationMailer
  
    # Subject can be set in your I18n file at config/locales/en.yml
    # with the following lookup:
    #
    #   en.form_mailer.message_received.subject
    #
    def message_received(contact)
      @contact = contact
      @url = 'http://example.com'
      @greeting = "Hello"
  
  
      mail(to: @contact.email, subject: 'Thanks for contacting Halstead Tire LLC')
  
    end
    def new_web_message
  
  
      @greeting = "Hello"
      @email = '&lt;mymail@gmail.com&gt;'
      @website = 'http://www.example.com'
  
  
  
      mail(to: @email, subject: 'A new message from your website.' )
    end
  
  
  
  end
  </code></pre>
  
  <p>The view new_web_message.html.erb:</p>
  
  <pre><code>&lt;h1&gt;&lt;%= @greeting %&gt; Nathan,&lt;/h1&gt;
  
  &lt;p&gt;
    Someone's requested some information from your site. Please go to &lt;%= @website %&gt; to get the details.
  &lt;/p&gt;
  &lt;body&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>I have the emails sending correctly and they are delivering as expected. The message for the Contact is working perfectly. I would like to have the Admin*(new_web_message)* email take the Contact's information and send it. How do I pass those values into the view? I want to take all the contact_params*(from contacts_controller)* and put them in the email to the admin. I realize this might be a simple and silly mistake but I can't figure it out. Is it possible to do this all in one mailer or do i need to have separate mailers for the contact and the admin*(new_web_message)*? *As i said the emails are working fine set up in one Mailer right now but I just need to get the data from the form passed into the admin**(new_web_message)* email.</p>
  
  <p>In the Contact email it works to access the data like so as is obvious in the mailer:</p>
  
  <pre><code>@contact.email
  @contact.phone_number
  @contact.message
  </code></pre>
  
  <p>and so forth. Why doesn't this same theory work in the Admin email??</p>
  
  <p>Does my mailer need to be named the same as the controller?(contacts_mailer) instead of (FormMailer) as i have it named now?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT:</p>
  
  <p>So I implemented Ryan's code in my app but now I get:</p>
  
  <pre><code> NoMethodError in Contacts#create
  </code></pre>
  
  <p>Here is the new_web_message.html.erb</p>
  
  <pre><code>    &lt;h1&gt;&lt;%= @greeting %&gt; Nathan,&lt;/h1&gt;
  
  &lt;p&gt;
    Someone's requested some information from your site. Please go to &lt;%=     @website %&gt; to get the details.
  &lt;/p&gt;
  &lt;body&gt;
  &lt;p&gt;&lt;%= @contact.first_name %&gt;&lt;/p&gt;
  &lt;p&gt;&lt;%= @contact.last_name %&gt;&lt;/p&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>Here is the error shown when i submit the form:</p>
  
  <pre><code>Showing /Users/evan/htire/app/views/form_mailer/new_web_message.html.erb where line #7 raised:
  
  undefined method `first_name' for nil:NilClass
  Extracted source (around line #7):
  
  
  &lt;/p&gt;
  &lt;body&gt;
  &lt;p&gt;&lt;%= @contact.first_name %&gt;&lt;/p&gt; (line 7)
  &lt;p&gt;&lt;%= @contact.last_name %&gt;&lt;/p&gt; (line 8)
  
  &lt;/body&gt;
  </code></pre>
  
  <p>Apparently I need to define methods somewhere but I'm running out of ideas. (yes I'm kinda new at rails)</p>
  
  <p>Here's the model contact.rb</p>
  
  <pre><code>class Contact &lt; ApplicationRecord
  
      def new_web_message
          first_name = first_name
          last_name = last_name
  
      end
  
  
          attr_accessor :first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message  
  
  
  
      validates_presence_of :first_name
      validates_presence_of :last_name
      validates_presence_of :phone_number
      validates_presence_of :message
  
  end
  </code></pre>
  
  <p>The updated mailer using Ryan's suggestion:</p>
  
  <pre><code>class FormMailer &lt; ApplicationMailer
  
    # Subject can be set in your I18n file at config/locales/en.yml
    # with the following lookup:
    #
    #   en.form_mailer.message_received.subject
    #
    def message_received(contact)
      @contact = contact
      @url = 'http://halsteadtire.net'
      @greeting = "Hello"
  
  
      mail(to: @contact.email, subject: 'Thanks for contacting Halstead Tire LLC')
  
    end
    def new_web_message(contact, params)
      @contact = contact
  
      @greeting = "Hello"
      @email = '&lt;halsteadtire1@gmail.com&gt;'
      @website = 'http://www.halsteadtire.net/contacts'
  
  
      mail(to: @email, subject: 'A new message from your website.' )
    end
  
  
  
  end
  </code></pre>
  
  <p>Updated controller using Ryan's code:</p>
  
  <pre><code>class ContactsController &lt; ApplicationController
    before_action :set_contact, only: [:show, :edit, :update, :destroy]
  
    # GET /contacts
    # GET /contacts.json
    def index
      @contacts = Contact.all
    end
  
    # GET /contacts/1
    # GET /contacts/1.json
    def show
    end
  
  
  
    # GET /contacts/new
    def new
      @contact = Contact.new
    end
  
    # GET /contacts/1/edit
    def edit
    end
  
    # POST /contacts
    # POST /contacts.json
    def create
      @contact = Contact.new(contact_params)
  
      respond_to do |format|
        if @contact.save
  
  
          FormMailer.message_received(@contact).deliver_now 
          FormMailer.new_web_message(@email, params).deliver_now
          format.html { redirect_to thank_you_path, notice: 'Your message was sent.' }
          format.json { render :show, status: :created, location: @contact }
        else
          format.html { render :new }
          format.json { render json: @contact.errors, status: :unprocessable_entity }
        end
      end
    end
  
  
  
    # PATCH/PUT /contacts/1
    # PATCH/PUT /contacts/1.json
    def update
      respond_to do |format|
        if @contact.update(contact_params)
          format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
          format.json { render :show, status: :ok, location: @contact }
        else
          format.html { render :edit }
          format.json { render json: @contact.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /contacts/1
    # DELETE /contacts/1.json
    def destroy
      @contact.destroy
      respond_to do |format|
        format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_contact
        @contact = Contact.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def contact_params
        params.require(:contact).permit(:first_name, :last_name, :vehicle_type, :tire_size, :current_tire, :phone_number, :email, :message)
      end
  end
  </code></pre>
  
  <p>What am I doing wrong?</p>