使用Rails / HAML的accept_nested_attributes形式的Action Mailer

时间:2014-08-26 18:07:45

标签: ruby-on-rails ruby actionmailer

我的模型设置:

class Contact < ActiveRecord::Base
  belongs_to :restaurant
end


class Restaurant < ActiveRecord::Base
  has_many :contacts
  accepts_nested_attributes_for :contacts
end

联系课程,尝试定位::email

class Contact < ActiveRecord::Base {
               :id => :integer,
       :first_name => :string,
        :last_name => :string,
             :role => :string,
            :email => :string,
            :phone => :string,
  :webmaster_email => :string,
    :restaurant_id => :integer,
       :created_at => :datetime,
       :updated_at => :datetime
}

我的创作动作:

  def create
    stripe_token = PayingCustomer.build(restaurant_params)
    @restaurant = Restaurant.new(restaurant_params)
    @restaurant.payment_profile_id = stripe_token.id
    @restaurant.generate_token
    respond_to do |with|
      if @restaurant.payment_profile_id != nil && @restaurant.save
        RestaurantMailer.signup_confirmation(@restaurant).deliver
        with.html {redirect_to restaurants_path(token: @restaurant.token)}
      else
        with.html {render :new }
      end
    end
  end

我不确定如何定位下面表单中输入的电子邮件:

 =form_for @restaurant, html: {id: 'signup-form', multipart: true} do |f|
      =field_set_tag %q{First, tell us about your restaurant.}, class: 'restaurant' do
        =label_tag '', '', for: 'restaurant_name'
        =f.text_field :name, placeholder: 'Restaurant Name'
        =label_tag '', '', for: 'restaurant_address'
        =f.text_field :address, placeholder: 'Street Address'
        =label_tag '', '', for: 'restaurant_city'
        =f.text_field :city, placeholder: 'City'
        =label_tag '', '', for: 'restaurant_state_province'
        =f.text_field :state_province, placeholder: 'State/Province'
        =label_tag '', '', for: 'restaurant_country'
        =select_tag 'restaurant[country]', "<option value='canada'>Canada</option><option value='usa'>United States</option>".html_safe, id: 'country'

      =field_set_tag %q{Provide us with your contact info.}, class: 'contact' do
        =f.fields_for :contacts do |contact_fields|
          =label_tag '', ''
          =contact_fields.text_field :first_name, placeholder: 'First Name'
          =label_tag '', ''
          =contact_fields.text_field :last_name, placeholder: 'Last Name'
          =label_tag '', ''
          =contact_fields.text_field :role, placeholder: 'Role at Restaurant'
          =label_tag '', ''
          =contact_fields.text_field :email, placeholder: 'Email'
          =label_tag '', ''
          =contact_fields.text_field :phone, placeholder: 'Phone'
          =label_tag '', ''
          =contact_fields.text_field :webmaster_email, placeholder: "Webmaster's Email (optional)"

我的目标是=contact_fields.text_field :email, placeholder: 'Email'

我试图找出如何在我的邮件中写这个,通过当前的设置,我得到了wrong number of arguments (1 for 2)

我的问题就在这里:

class RestaurantMailer < ActionMailer::Base
  default from: "from@example.com"

  def signup_confirmation(restaurant, contact) 
    @greeting = "Hi"
    @restaurant = restaurant  
    @contact = contact
    mail to: contact.email, subject: "Sign up Confirmation"
  end
end

编辑:

  def signup_confirmation(restaurant)
    @greeting = "Hi"
    @restaurant = restaurant
    @contact = restaurant.contact
    mail to: @contact.email, subject: "Sign up Confirmation"
  end

结果:

NoMethodError in RestaurantsController#create
undefined method `contact' for #<Restaurant:0x007fd686c593b0>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

看起来您的问题出现在RestaurantMailer.signup_confirmation(@restaurant).deliver中,因为您传递了一个参数,而signup_confirmation需要两个参数。

在这种情况下你可以写:

def signup_confirmation(restaurant) 
  @greeting = "Hi"
  @restaurant = restaurant  
  @contact = restaurant.contacts.first
  mail to: @contact.email, subject: "Sign up Confirmation"
end