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

时间:2013-02-17 23:04:09

标签: ruby-on-rails-3 routing actionmailer mailer

我在请求此路线时遇到此错误: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'

1 个答案:

答案 0 :(得分:0)

啊,非常有用的错误消息!

在第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 =&gt;“hello”,:host   =&GT; “嗒嗒”)   =&GT; “http://hello.blah/appointment/6a5cc9a0-5b8f-0130-9883-0026080ebf68

普罗蒂普

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