Ruby Rails - Action Mailer 400 错误请求

Ruby on Rails - Action Mailer 400 Bad request

当我尝试在 Rails 4.1 和 Action Mailer 上发送带有 Ruby 的表单时出现以下错误:

Started POST "/contact_sponsors" for 127.0.0.1 at 2018-05-17 16:22:35 -0300
Processing by ContactSponsorsController#create as JS
  Parameters: {"utf8"=>"✓", "contact_sponsor"=>{"company_name"=>"godocu@mailinator.com", "company_cnpj"=>"dahok@mailinator.com", "company_cep"=>"watyhib@mailinator.com", "company_address"=>"dofimocah@mailinator.com", "company_number"=>"duki@mailinator.com", "company_existence"=>"mamopiwir@mailinator.net", "company_sponsor_type"=>"", "responsible_name"=>"qomukuf@mailinator.com", "responsible_email"=>"ryhohiqe@mailinator.net", "additional_infos"=>"Eum sit nesciunt occaecat facere delectus vel aut sint animi pariatur Ut ipsum officia ratione est enim est"}}
  HomeConfig Load (4.1ms)  SELECT  `home_configs`.* FROM `home_configs`   ORDER BY `home_configs`.`id` ASC LIMIT 1
   (0.2ms)  BEGIN
   (0.4ms)  ROLLBACK
  Rendered text template (0.0ms)
Completed 400 Bad Request in 16ms (Views: 0.8ms | ActiveRecord: 4.8ms)

谁能帮帮我?我试图通过多种方式解决这个问题,但无济于事。这是我的控制器、邮件程序和模型:

class ContactSponsorsController < InheritedResources::Base
  def create
    @contact_sponsor = ContactSponsor.new
    if @contact_sponsor.save
      ContactSponsorMailer.delay_for(10.seconds, retry: true).create(@contact_sponsor)
      render nothing: true, status: 200
    else
      render nothing: true, status: 400
    end
  end
end

class ContactSponsorMailer < ActionMailer::Base
  default from: "Facens Lince <no-reply@facens.br>"

  def create(contact_sponsor)
    @contact_sponsor = contact_sponsor
    mail(to: "felipe.marcon@atua.ag", subject: "Contato Através do Site")
  end
end

class ContactSponsor < ActiveRecord::Base
  validates :company_cnpj, :company_address, :company_number, :company_size, :company_existence, :company_sponsor_type, :responsible_name, :responsible_email, presence: true
end

谢谢。

@contact_sponsor.save 返回 false 因此它 returns 400。 当 returns 为 false 时检查 @contact_sponsor.errors.full_messages 的错误,或使用 save! 引发异常(气馁,仅将其用于调试)并查看出了什么问题。

根据显示的日志和共享的代码片段,似乎属性 "company_sponsor_type" 似乎是空白字符串,并且在模型中您已经写入了相同属性的存在,因此它正在获取回滚。

因此,要么删除验证,要么在 company_sponsor_type 键中发送一些值。

看,在 ContactSponsor 中你有 validate 那些必须存在的数据,但你没有传递任何意味着空的数据,这就是为什么

Completed 400 Bad Request

为此,您需要为 strong parameters 创建一个私有方法,请看下面的

class ContactSponsorsController < InheritedResources::Base
    def create
        @contact_sponsor = ContactSponsor.new(contact_sponsor_params)
        if @contact_sponsor.save
            ContactSponsorMailer.delay_for(10.seconds, retry: true).create(@contact_sponsor)
            render nothing: true, status: 200
        else
            render nothing: true, status: 400
        end
    end

    private

    def contact_sponsor_params
        params.require(:contact_sponsor).permit(:company_cnpj, :company_address, :company_number, :company_size, :company_existence, :company_sponsor_type, :responsible_name, :responsible_email)
    end
end

我认为它会起作用。