luissimo luissimo - 5 months ago 10
Ruby Question

Nomethoderror on form using mail_form gem for second form

i am building a website for which i already made a working contact form using the mail_form gem. But now i wan't to create another form somewhere on the website, also using the mail_form gem.

I made a new controller, a new model, new views and put up the routes.

boosts_controller.rb

class BoostsController < ApplicationController

def new
@contact = Contact.new
end

def create
@contact = Contact.new(params[:boost])
@contact.request = request
if @contact.deliver
flash.now[:notice] = "Thank you very much, we will contact you on your email with further instructions"
else
flash.now[:error] = "Something went wrong, please try again."
render :new
end
end

end


Model: boost.rb

class Boost < MailForm::Base
attribute :paypal_transaction_reference, :validate => true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message
attribute :account_name, :validate => true
attribute :account_password, :validate => true
attribute :account_password_confirmation, :validate => true
attribute :nickname, :captcha => true

# Declare the e-mail headers. It accepts anything the mail method
# in ActionMailer accepts.
def headers
{
:subject => "Form",
:to => "xxxx@hotmail.com, xxxx@gmail.com",
:from => %("#{account_name}", <#{email}>)
}
end
end


boosts/new.html.erb

<div class="col-md-12 text-center">
<div class="sign-box">
<header class="sign-title"> form</header>

<%= form_for @boost do |f| %>

<div class="field form-group">
<%= f.label :paypal_transaction_reference, class: 'float-left' %>
<%= f.text_field :paypal_transaction_reference, required: true, class: 'form-control', autofocus: true %>
</div>

<div class="field form-group">
<%= f.label :email, class: 'float-left' %>
<%= f.email_field :email, class: 'form-control', required: true, autocomplete: "off" %>
</div>

<div class="field form-group">
<%= f.label :account_name, class: 'float-left' %>
<%= f.text_field :account_name, class: 'form-control', required: true, autocomplete: "off" %>
</div>

<div class="field form-group">
<%= f.label :account_password, class: 'float-left' %>
<%= f.password_field :account_password, class: 'form-control',required: true, autocomplete: "off" %>
</div>

<div class="field form-group">
<%= f.label :account_password_confirmation, class: 'float-left' %>
<%= f.password_field :account_password_confirmation, class: 'form-control', required: true, autocomplete: "off" %>
</div>

<div class="field form-group">
<%= f.label :message, class: 'float-left' %>
<%= f.text_area :message, as: :text, class: 'form-control', autocomplete: "off" %>
</div>

<div class="hidden">
<%= f.label :nickname %>
<%= f.text_field :nickname, hint: 'leave this field blank' %>
</div>


<%= f.submit 'Send', class: 'btn btn-rounded' %>
<% end %>
</div>
</div>


routes.rb from the working contact form plus the new one.

match '/boosts', to: 'boosts#new', via: 'get'
resources :boosts, only: [:new, :create]

match '/contacts', to: 'contacts#new', via: 'get'
resources :contacts, only: [:new, :create]


The error i get on localhost is:

undefined method `paypal_transaction_reference' for #<Contact:0x007f9dbbb307d0>


The error log

Completed 500 Internal Server Error in 31ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined method `paypal_transaction_reference' for #<Contact:0x007f9dbbb307d0>):
6:
7: <div class="field form-group">
8: <%= f.label :paypal_transaction_reference, class: 'float-left' %>
9: <%= f.text_field :paypal_transaction_reference, required: true, class: 'form-control', autofocus: true %>
10: </div>
11:
12: <div class="field form-group">
app/views/boosts/new.html.erb:9:in `block in _app_views_boosts_new_html_erb___4155220419788953995_70157717804640'
app/views/boosts/new.html.erb:5:in `_app_views_boosts_new_html_erb___4155220419788953995_70157717804640'

Answer

IN Boost Controller

@contact = Contact.new

Should be

@boost = Boost.new