Ruby on rails 设置create方法以自动发送欢迎电子邮件,必须将哈希作为参数错误传递

Ruby on rails 设置create方法以自动发送欢迎电子邮件,必须将哈希作为参数错误传递,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,我正在用RubyonRails创建一个具有mailer功能的博客。我想在新用户注册后向他们发送一封欢迎电子邮件。不幸的是,在我当前的代码设置中,我收到了一个错误(“分配属性时,必须将哈希作为参数传递。”)!我已经用尽了我所有的脑力来纠正这个问题,我将非常感谢任何人能给我的任何帮助 这是我的代码:订阅者控制器 class SubscribersController < ApplicationController before_action :set_subscriber, only: [:s

我正在用RubyonRails创建一个具有mailer功能的博客。我想在新用户注册后向他们发送一封欢迎电子邮件。不幸的是,在我当前的代码设置中,我收到了一个错误(“分配属性时,必须将哈希作为参数传递。”)!我已经用尽了我所有的脑力来纠正这个问题,我将非常感谢任何人能给我的任何帮助

这是我的代码:订阅者控制器

class SubscribersController < ApplicationController
before_action :set_subscriber, only: [:show, :edit, :update, :destroy]

 respond_to :html

  def send_welcome
  NotificationMailer.welcome(params[:id]).deliver
    redirect_to new_subscriber_path
  end  

  def index
    @subscribers = Subscriber.all
    respond_with(@subscribers)
  end

  def show
    @subscriber = Subscriber.find(params[:id])
    respond_with(@subscriber)
  end

  def new
    @subscriber = Subscriber.new
    respond_with(@subscriber)
  end

  def edit
   @subscriber = Subscriber.find(params[:id])
  end

  def create
    @subscriber = Subscriber.new(subscriber_params)
    if @subscriber.save
        NotificationMailer.welcome(@subscriber)
        redirect_to posts_path, notice: "You have successfully subscribed."

    else 
      render 'new'
    end 

  end

  def update
    @subscriber = Subscriber.find(params[:id])
    @subscriber.update(subscriber_params)
    respond_with(@subscriber)
  end

  def destroy
    @subscriber = Subscriber.find(params[:id])
    @subscriber.destroy
    respond_with(@subscriber)
  end

  private
    def set_subscriber
      @subscriber = Subscriber.find(params[:id])
    end

    def set_email
      @email = Subscriber.find(params[:email])
    end  

    def subscriber_params
      params.require(:subscriber).permit(:name, :email, :mobile)
    end
end

Notification_mailer - Welcome Method

    class NotificationMailer < ActionMailer::Base
  default from: "myemail@gmail.com"

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.notification_mailer.welcome.subject
  #
  def welcome(subscriber)
    @subscriber = Subscriber.new(subscriber)
    @url = 'http://localhost:3000/posts'
    mail :to => "#{@subscriber.name} <#{@subscriber.email}>",
          :from => "myemail@gmail.com",
          :subject => 'Welcome to My Blog'
    end
我目前正在使用rails 4,并且安装了Desive,因此我可以作为站点管理员登录

提前感谢您发布的任何帮助

更新

为了让一切正常工作,我对通知邮件器进行了更改,并在订阅者控制器中重新配置了create方法。代码如下:

通知邮递员

   def welcome(subscriber)
   @subscriber = subscriber
   @url = 'http://localhost:3000/posts'
   mail to:  "#{@subscriber.name} <#{@subscriber.email}>",
      from:  "wildgenius99@gmail.com",
      subject: 'Welcome to My Blog'
   end

请您也发布错误消息好吗?谢谢您正在将实际的订户对象传递给邮件程序,然后调用
subscriber.new(@subscriber)
,这将从您现有的订户对象创建一个新订户。尝试将
@subscriber=subscriber.new(subscriber)
的行替换为
@subscriber=subscriber
@Sixty4Bit以下是错误消息:ArgumentError in subscribers controller#分配属性时创建,您必须传递一个散列作为参数。@MikeManfrin我尝试了您的建议,更改后应用程序仍然不会发送电子邮件。您的
表单是什么样子的,它将
订户参数
馈送到
\create
。错误消息可能表示问题在这里:
@subscriber=subscriber.new(subscriber_参数)
<h1>NotificationMailer#welcome</h1>

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
   </head>
  <body>
    <h1>Welcome to My Blog, <%= @subscriber.name %></h1>
    <p>
      You have successfully subscribed! <br>
    </p>
    <p>
   Please visit the site, read the post, and feel free to leave a comment,        
      just follow this link: <%= @url %>.   
     </p>
     <p>Thanks for joining and have a great day!</p>
  </body>
  </html>
config.action_mailer.delivery_method = :smtp
 config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  #domain:               'example.com',
  user_name:            'myemail@gmail.com',
  password:             'Password',
  authentication:       'plain',
  enable_starttls_auto: true  }
   def welcome(subscriber)
   @subscriber = subscriber
   @url = 'http://localhost:3000/posts'
   mail to:  "#{@subscriber.name} <#{@subscriber.email}>",
      from:  "wildgenius99@gmail.com",
      subject: 'Welcome to My Blog'
   end
   def create
   @subscriber = Subscriber.new(subscriber_params)
   respond_to do |format|
   if @subscriber.save

    NotificationMailer.welcome(@subscriber).deliver
    format.html { redirect_to posts_path, notice: 
    'Subscriber was successfully      created.' }
    format.json { render :show, status: :created, location: @subscriber }
    else
    format.html { render :new }
    format.json { render json: @subscriber.errors, status:     
    :unprocessable_entity }
    end
   end
  end