0

请求此路线时出现此错误: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'
4

1 回答 1

0

啊,有用的错误信息就这么多!

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

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 => "hello", :host => "blah") => " http://hello.blah/appointment/6a5cc9a0-5b8f-0130-9883-0026080ebf68 "

专家提示

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

于 2013-02-18T00:32:13.440 回答