题
使用产生的代码restful_authentication,我们通常得到'localhost'URL的注册和活邮件。我试图建立这样的事情,而不是:
def signup_notification(user, host)
setup_email(user)
@subject += 'Please activate your new account'
@body[:url] = "http://#{host}/activate/#{user.activation_code}"
end
然而,由于实际调用传递邮件是在观察,我不能这样做:
UserMailer.deliver_signup_notification(user, request.host_with_port)
由于请求的方法不可用在模型。什么是最好的方式去这样做?
我以为关于储存网址,每域在其文件,然后装上启动,但随后的港口可以改变的,所以它不会的工作。
或者,我以为关于创建一个静态的类变量的地方,是设在加载应用程序,但我不知道如何去这样做。是的请求的方法可用于初始?
解决方案
尝试current_request插件,在 http://codeshooter.wordpress.com/2009/08/描述
为我工作
其他提示
使用ActiveMailer,我设置这在config/envrionments/production.rb
config.action_mailer.default_url_options = { :host => "my.host.com" }
在该模型app/models/event_mailer.rb
def new_event(event)
recipients EventMailer::NO_REPLY
bcc event.emails('new_event')
from EventMailer::FROM_EMAIL
subject "#{EventMailer::SUBJECT_HEADER} Event Updated :: #{event.title}"
content_type "text/plain"
body :event => event
end
和然后在所述邮件收发器视图app/views/event_mailer/new_event.rb
You can view the event by going to: <%= url_for(:controller => "events", :action => "show", :id => @event.id, :only_path => false) %>
在邮件生成:
You can view the event by going to: http://my.host.com/events/11
我有个同样的问题和这里的我做了什么
在用户的模型我址:主attr_accessible,然后我加入这个方法
def host=(value) write_attribute :host, value end
在用户控制器、在方法发送电子邮件等创造和激活我把
@user.host = request.host_with_port
自从我还是个菜鸟,上轨道所以我真的希望有人会拿出一个最佳实践这个问题的解决办法。
的主机名的服务器是通过在请求的主机头发现访问。你不应该访问它的模型(假设Rails是理智)。但是,如果你能在控制器访问请求的对象,当你从数据库中拉模型对象,你可以插入值到模型领域。
不隶属于 StackOverflow