default_url_options 和rails 3
由于 ActionController::Base#default_url_options 已弃用,我想知道如何在 Rails3 中设置默认 url 选项。默认 url 选项不是静态的,而是取决于当前请求。
http://apidock.com/rails/ActionController/Base/default_url_options
谢谢, 科林
As ActionController::Base#default_url_options is deprecated, I wonder how to set default url options in rails3. The default url options are not static but dependent of the current request.
http://apidock.com/rails/ActionController/Base/default_url_options
Thanks,
Corin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要为当前请求设置 url 选项,请在控制器中使用类似以下内容:
现在,:profile => current_profile 将自动合并到路径/url 参数。
路由示例:
只需写入:
如果 current_profile 将 to_param 设置为“lucas”:
To set url options for current request use something like this in your controller:
Now, :profile => current_profile will be automerge to path/url parameters.
Example routing:
Just write:
and if current_profile has set to_param to 'lucas':
我相信现在首选的方法是告诉路由器处理这个问题:
您可以将此行放在
routes.rb
或初始化程序中。无论您喜欢哪个。如果值根据您的环境而变化,您甚至可以将其放入您的环境配置中。I believe the preferred method is to now tell the router to handle this:
You can put this line in either
routes.rb
or an initializer. Whichever you would prefer. You could even put it in your environment configs if the values change based on your environment.该 apidock.com 链接具有误导性。 default_url_options 并未被弃用。
http://guides.rubyonrails.org/action_controller_overview.html#default_url_options
That apidock.com link is misleading. default_url_options is not deprecated.
http://guides.rubyonrails.org/action_controller_overview.html#default_url_options
特别是对于 Rails 3,规范的方法是将
default_url_options
方法添加到ApplicationController
中。我只需要自己解决这个问题,所以我知道它是有效的。
这是改编自 Rails 3 指南:
http://guides.rubyonrails.org/v3.2.21/action_controller_overview.html#default_url_options
For Rails 3 specifically, the canonical way to do it is by adding a
default_url_options
method to yourApplicationController
.I just had to figure this out myself, so I know it works.
This is adapted from the Rails 3 Guide:
http://guides.rubyonrails.org/v3.2.21/action_controller_overview.html#default_url_options
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
在developemnt.rb / test.rb中,可以更简洁如下:
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
In the developemnt.rb / test.rb, can be more concise as following: