以Rails 5形式联系表单数据

时间:2016-08-04 01:41:01

标签: ruby-on-rails ruby forms email

我有其中一个时刻。我的问题可能很简单,但我还没弄明白。

我有一个我正在开发的Rails 5应用程序。它包含联系人填写和提交的联系表格。

接下来,我将ActionMailer设置为向联系人发送电子邮件,通知他们他们的消息已收到并正在审核中。我还将ActionMailer设置为向应该包含联系表单数据的网站管理员发送电子邮件。这是我遇到问题的地方。如何将联系表单数据传递到我的电子邮件视图?

这是contacts_controller:

class ContactsController < 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

The Mailer:

    class FormMailer < 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 = '<mymail@gmail.com>'
    @website = 'http://www.example.com'



    mail(to: @email, subject: 'A new message from your website.' )
  end



end

视图new_web_message.html.erb:

<h1><%= @greeting %> Nathan,</h1>

<p>
  Someone's requested some information from your site. Please go to <%= @website %> to get the details.
</p>
<body>

</body>

我已正确发送电子邮件,并按预期交付。联系人的消息非常有效。我想让Admin *(new_web_message)*电子邮件收到联系人的信息并发送给我。如何将这些值传递到视图中?我想取所有contact_params *(来自contacts_controller)*并将它们放入电子邮件给管理员。我意识到这可能是一个简单而愚蠢的错误,但我无法弄明白。是否可以在一个邮件程序中执行此操作,或者我是否需要为联系人和管理员*(new_web_message)*设置单独的邮件程序? *正如我所说的那样,电子邮件在一个Mailer中正常工作,但我只需要将表格中的数据传递给admin **(new_web_message)*电子邮件。

在联系电子邮件中,它可以访问数据,就像邮件中显而易见的那样:

@contact.email
@contact.phone_number
@contact.message

等等。为什么这个理论在管理员电子邮件中不起作用?

我的邮件程序是否需要与控制器命名相同?(contacts_mailer)而不是(FormMailer),因为我现在命名它?

谢谢!

编辑:

所以我在我的应用程序中实现了Ryan的代码,但现在我得到了:

 NoMethodError in Contacts#create

这是new_web_message.html.erb

    <h1><%= @greeting %> Nathan,</h1>

<p>
  Someone's requested some information from your site. Please go to <%=     @website %> to get the details.
</p>
<body>
<p><%= @contact.first_name %></p>
<p><%= @contact.last_name %></p>

</body>

以下是我提交表单时显示的错误:

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):


</p>
<body>
<p><%= @contact.first_name %></p> (line 7)
<p><%= @contact.last_name %></p> (line 8)

</body>

显然我需要在某个地方定义方法,但我已经没有想法了。 (是的,我在铁路上有点新鲜)

这是模型contact.rb

class Contact < 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

更新后的邮件使用了Ryan的建议:

class FormMailer < 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 = '<halsteadtire1@gmail.com>'
    @website = 'http://www.halsteadtire.net/contacts'


    mail(to: @email, subject: 'A new message from your website.' )
  end



end

使用Ryan的代码更新控制器:

class ContactsController < 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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该能够将参数传递到new_web_message。将您的方法更改为:

def new_web_message(contact)
  @contact = contact
  #rest of your code
end

然后打电话使用:

FormMailer.new_web_message(@contact).deliver_now

如果您想要所有参数(即使是那些你没有保存到Contact的参数),只需将其添加为另一个参数:

def new_web_message(contact, params)
  ...
end

FormMailer.new_web_message(@email, params).deliver_now

免责声明:我不知道这是否适用于Rails 5(它应该),但这可以在Rails 4.2中使用。