-1

在我的生产服务器上,我有一个 rails 应用程序和一个登录页面,位于我的应用程序的公共文件夹中。Nginx 配置为监听 2 个域名。因此,如果域名是,例如 is railsapp.com,它会打开 rails 应用程序的索引页面,如果域名,例如 is landing.com,它会打开登录页面。在登陆页面的底部,有一个联系表格,这是我的应用程序控制器,它有一个通过以下方式发送电子邮件的方法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: 'my-email@landing.com',
      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:             'landing.noreply@gmail.com',
        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

development一切正常的情况下,我收到了电子邮件,而在生产中我收到了 404,并且 url 是:https://landing.com/contact-us。此操作的路线是:

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

为什么网址包含/contact-us?可以做些什么来让它发挥作用?谢谢。

4

1 回答 1

0

解决此问题的更好方法是在根路径上使用约束:

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

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

这避免了每次访问根路径时的额外重定向。这也意味着您的控制器和视图无需担心使用哪个路径,只需使用root_path.

于 2017-04-29T17:58:49.643 回答