0

I just built a really simple mailer using Rails 6 and when i deployed to production the error popped up, saying i was missing the User param is missing, fyi, is just a landpage with the form where you do not have to log in to send anything, you just fill it out and we get the information thru the email. here is the form view:

<div class="form">
  <div class="row">
    <%= form_for :contact, url: send_contact_path do |f| %>
    <%= f.text_field :name, placeholder: "Full Name" %>
    <%= f.text_field :email, placeholder: "Email Address" %>
    <%= f.text_field :phone_number, placeholder: "Phone Number" %>
  </div>

  <div class="row">
    <%= f.text_field :partner_name, placeholder: "Partner's Full Name" %>
    <%= f.text_field :partner_email, placeholder: "Partner's Email" %>
    <%= f.text_field :partner_phone, placeholder: "Partner's Phone" %>
  </div>
  
  <div class="row">
    <%= f.select(:category, categories, placeholder: "Category") %>
    <%= f.number_field :paid_amount, step: 0.01, placeholder: "Amount Paid" %>
  </div>
  <%= f.text_area :message, placeholder: "Anything you would want to say" %>
  <%= f.submit "Send Info" %>
  <% end %>
</div>

here is the controller:

  def create_contact
    contact = User.new(contact_params)
    if contact.save
      send_contact(contact)
      send_copy(contact)
      flash[:success] = 'Message sent successfully!'
    else
      redirect_to root_path
      flash[:notice] = 'Error, try again'
    end
  end

  private

  def contact_params
    params.require(:user).permit(:name, :email, :phone_number, :partner_name, :partner_email, :partner_phone, :message, :amount_paid, :category)
  end

  def send_contact(contact)
    mail = Mailjet::Send.create(messages: [{
      "From"=> {
        "Email"=>"email",
        "Name"=>"name"
      },
      "To"=> {
        "Email"=>"email",
        "Name"=>"name"
      },
      "Subject"=>"subject",
      "HTMLPart"=>template(contact)
    }])
  end

thanks, im using pg as the db and mailjet as the mailing service

user14102083
  • 39
  • 1
  • 5
  • what is the actual error that appears on the logs? – diego Sep 28 '21 at 17:39
  • FATAL -- : [dcc1dfcf-5bfb-4b59-9318-ddc30333a8e1] 2021-09-28T17:38:18.680224+00:00 app[web.1]: [dcc1dfcf-5bfb-4b59-9318-ddc30333a8e1] ActionController::ParameterMissing (param is missing or the value is empty: user): – user14102083 Sep 28 '21 at 17:39

1 Answers1

0

The issue with your code is the following line:

<%= form_for :contact, url: send_contact_path do |f| %>

Namely the first parameter :contact. What this does is that the object you receive through the html form is contact instead of user, then in strong parameters you get the error user missing, because it is not there, if you would replace it with require contact it would also work as expected.

Here is #form_for api docs for reference: https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for