简体   繁体   中英

Ruby on Rails - Action Mailer 400 Bad request

I'm having the following error when I try send a form with Ruby on Rails 4.1 and Action Mailer:

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)

Anyone can help me? I've tried to sort this out in many ways but to no avail. Here is my Controller, Mailer and Model:

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

Thanks.

@contact_sponsor.save returned false therefore it returns 400. Check the errors you had with @contact_sponsor.errors.full_messages when it returns false, or use save! to raise an exception (discouraged, use it only for debugging) and see what went wrong.

As per the logs shown and code snippet shared, it seems like the attribute "company_sponsor_type" seems to be blank string and in the model you have the written the presence true of the same attribute and thus it is getting rolled back.

So either remove the validation or send some value in the company_sponsor_type key.

Look, in the ContactSponsor you have validate those data which must be present but you have not passed any data that means empty that's why

Completed 400 Bad Request

and for this, you need to create a private method for strong parameters look at that below

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

I think it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM