Get 404 in production when sending email via pony while in development everything works fine

41 Views Asked by At

On my production server I have a rails app, and a landing page, that is located in the public folder of my app. Nginx is configured to listen to 2 domain names. So, if the domain name is, for example, is railsapp.com, it opens me the index page of the rails app, and if domain name, for example, is landing.com, it opens the landing page. On the landing, at the bottom of the page, there is a contact form, this is my application controller, that has a method to send emails via Pony:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  skip_before_action :verify_authenticity_token, if: :contact_us

  def contact_us
    name = params[:name]
    phone = params[:phone]
    email = params[:email]
    website = params[:website]
    body = params[:body]

    Pony.mail(
      from: email,
      to: '[email protected]',
      subject: "Landing. New mail from #{name}, phone number: #{phone}, website: #{website}",
      body: body,
      via: :smtp,
      via_options: {
        address:               'smtp.gmail.com',
        port:                  '587',
        enable_starttls_auto:  true,
        user_name:             '[email protected]',
        password:              'password',
        authentication:        :plain,
        domain:                'gmail.com'
      }
    )

    if Rails.env.production? && params[:email].present?
      redirect_to 'https://landing.com'
    elsif Rails.env.development? && params[:email].present?
      redirect_to '/landing'
    end
  end
end

In development everything works fine, I receive the email, while in production I get 404, and the url is: https://landing.com/contact-us. The route for this action is:

Rails.application.routes.draw do
  # other routes
  post '/contact-us' => 'application#contact_us', as: 'contact_us'
end

Why the url contains /contact-us? What can be done to make it work? Thanks.

1

There are 1 best solutions below

0
max On

A better way solve this is by using a constraint on the root path:

constraints host: 'railsapp' do
  root to: 'foo#bar'
end

constraints host: 'landing' do
  root to: 'some_controller#some_method'
end

This avoids an extra redirect for every visit to the root path. It also means that you controllers and views don't need to worry about which path to use and can just use root_path.