在我的应用程序.html.erb中,我有<%= render @objects %>
,它呈现了一堆_object.html.erb
部分,每个部分都有一个<%= link_to(object) %>
。当有人留下评论时,我在发送的电子邮件中呈现了相同的单个部分<%= render @object %>
,但我希望链接以服务器url开头。
我什么都试过了:
link_to(object)
url_for(object)
before_filter :set_mailer_host
def set_mailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
default_url_options[:host] = "example.com"
def default_url_options
{ host: 'example.com' }
end
什么都不管用。带或不带:only_links
最后,我只构建了一个愚蠢的助手,它将主机名前置到链接:
# application_controller.rb
before_filter { App::request=request }
# application_helper.rb
def hostify obj
"http://#{App::request.host_with_port}#{url_for obj}"
end
# _object.html.erb:
<%= link_to obj.title, hostify(object) %>
有正常的方法吗?
我真的不知道你试图链接到什么或"obj"指的是什么,但你可以做一些类似的事情:
link_to obj.title, {:host => 'example.com'}
或
link_to obj.title, {:controller => 'store', :action => 'view_cart', :host => 'example.com'}
好吧,花了一段时间之后,我想我开始明白了。
您必须使用restful路由,然后才能在Mailer操作中将default_url_options[:host] = host
设置为true,并在其他控制器的default_url_options
中将:only_path设置为true。加上:locale
。
在我看来:objects_url(object)
由于我有不同的主机名,所以在发送邮件时,我将request.host_with_port
作为参数从控制器传递给Mailer。
link_to
不接受参数,url_for()
只能从其中的一部分构建url。
def url_options
{ domain: 'example.com' }
end
或者如果你使用其他选项,最好合并:
def url_options
{ domain: 'example.com' }.merge(super)
end