1

In my contact.html, I have this code

            <div class="form">
                <form name="email-form" method="POST">
                    <label class="field-label" for="name">Name:</label>
                    <input class="w-input text-field-2" id="name" type="text" name="name" data-name="Name" required="required">
                    <label class="field-label" for="Email">Email:</label>
                    <input class="w-input text-field-2" id="email" type="email" name="email" data-name="Email" required="required">
                    <label class="field-label" for="Subject">Subject:</label>
                    <input class="w-input text-field-2" id="subject" type="text" name="subject" data-name="Subject">
                    <label class="field-label" for="Content">Text Message:</label>
                    <textarea class="w-input text-field-2 area" id="content" name="content" data-name="Text Area" required="required"></textarea>
                    <div>
                        <input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
                    </div>
                </form>
            </div>

This is under my controller

 def contact

 end

 def send_mail
    MessageMailer.new_message(contact_params).deliver
    redirect_to contact_path, notice: 'Your messages has been sent.'
 end

 private
   def contact_params
      params.require(:contact).permit(:name, :email, :subject, :content)
   end

under mailers/message_mailer.rb

class MessageMailer < ActionMailer::Base
   default from: "[email protected]"
   default to: "[email protected]"

   def new_message(contact)
      @contact = contact

      mail subject: @contact.subject
   end
end

and under my new_message.text.erb is this code

Name: <%= @contact.name %>
Email: <%= @contact.email %>
Message: <%= @contact.content %>

I am to send an email consisting user's name, email and message which is inputed and NOT saved in the database. When I pass the four parameters like this

def send_mail
    MessageMailer.new_message(:name, :email, :subject, :content).deliver
    redirect_to contact_path, notice: 'Your messages has been sent.'
end

it worked just fine but i was told to use only one parameter, seems like group the four parameters: name, email, subject, content as one (contact)

When I typed the info and hit the submit button, I get this error message param is missing or the value is empty: contact I presume that what caused this error is because my def contact is empty. So I added contact.new and @contact=contact.new and MessageMailer.new but this error occurs NoMethodError

How can I possibly fix this? What should I write under my def contact ?

1
  • contact.new doesn't work, it will be Contact.new Commented Apr 22, 2015 at 7:07

2 Answers 2

1

Your controller code is correct. You don't need Contact.new or something. The problem is with your <form>. What ends up in params depends on your form and in your form you don't have contact.

Instead of:

<input type="text" name="subject">

You have to do something like this:

<input type="text" name="contact[subject]">

And that for all the fields on your contact form.

Another option would be to use Rails' form helpers.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, this is a possibility too :)
1

contact[] is missing in your form, see below correct one:

        <div class="form">
            <form name="email-form" method="POST">
                <label class="field-label" for="name">Name:</label>
                <input class="w-input text-field-2" id="name" type="text" name="contact[name]" data-name="Name" required="required">
                <label class="field-label" for="Email">Email:</label>
                <input class="w-input text-field-2" id="email" type="email" name="contact[email]" data-name="Email" required="required">
                <label class="field-label" for="Subject">Subject:</label>
                <input class="w-input text-field-2" id="subject" type="text" name="contact[subject]" data-name="Subject">
                <label class="field-label" for="Content">Text Message:</label>
                <textarea class="w-input text-field-2 area" id="content" name="contact[content]" data-name="Text Area" required="required"></textarea>
                <div>
                    <input class="w-button button" type="submit" value="Submit Message" data-wait="Please wait...">
                </div>
            </form>
        </div>

1 Comment

i havnt' seen, stackoverflow doesn't reload the page to see it, I was preparing/formatting my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.