表单上的Nomethoderror使用mail_form gem作为第二种形式

时间:2016-06-07 10:39:26

标签: ruby-on-rails ruby ruby-on-rails-4 rubygems

我正在构建一个网站,我已经使用mail_form gem制作了一个工作联系表单。但现在我不想在网站上的某个地方创建另一个表单,也使用mail_form gem。

我制作了一个新的控制器,一个新的模型,新的视图并建立了路线。

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

型号: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

提升/ 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 加上新的。

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

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

我在localhost上遇到的错误是:

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

错误日志

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'

1 个答案:

答案 0 :(得分:1)

IN Boost Controller

@contact = Contact.new

Should be

@boost = Boost.new