Rails 3路由类型不匹配在Mailer视图中请求具有子域的路由的url时出错



请求此路由时出现此错误:direct_appointment_url(@appointment.uuid, :subdomain => @user.name)

direct_appointment_url(@appointment.uuid, :subdomain => @user.name)
TypeError: can't convert Array to String
from ...rvm/gems/ruby-1.9.3-p362@example/gems/actionpack-3.2.12/lib/action_dispatch/http/url.rb:55:in `match'

路由定义为:get "/appointment/:uuid" => "schedule#appointment", as: "direct_appointment"

传入路由匹配器的两个变量的值为:

appointment.uuid => "a52aff80-5b83-0130-987f-0026080ebf68"

@user.name => "example"

更多上下文:

ActionView::Template::Error (can't convert Array to String):
    13: Message: <%= @appointment.client_message %>
    14: <% end %>
    15:
    16: View online: <%= direct_appointment_url(@appointment.uuid, :subdomain => @user.name) %>
    17:
    18: To see your latest appointments, login at: <%= @login_url %>
    19: Your username is: <%= @user.email %>
  app/views/user_mailer/new_appointment_email.text.erb:16:in `_app_views_user_mailer_new_appointment_email_text_erb__686277782469675039_70200502266100'
  app/mailers/user_mailer.rb:13:in `block in new_appointment_email'
  app/mailers/user_mailer.rb:12:in `new_appointment_email'
  app/controllers/appointments_controller.rb:43:in `block in create'
  app/controllers/appointments_controller.rb:42:in `create'

啊,有用的错误消息到此为止!

查看第55行的https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/url.rb的源代码,(尽管为空)前几行显示了本应引发的错误:

if options[:host].blank? && options[:only_path].blank?
  raise ArgumentError, 'Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true'
end

一旦我指定了一个主机,它就工作了:

direct_appointment_url(@appointment.uuid,:subdomain=>"你好",:host=>"废话")=>"http://hello.blah/appointment/6a5cc9a0-5b8f-0130-9883-0026080ebf68"

Protip

不要向配置文件提供可能的主机阵列:config.action_mailer.default_url_options = { :host => ['lvh.me:3000', 'blah.dev'] }

最新更新