default_url_options 和rails 3

发布于 2024-11-09 09:47:48 字数 299 浏览 6 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

演出会有结束 2024-11-16 09:47:48

要为当前请求设置 url 选项,请在控制器中使用类似以下内容:

class ApplicationController < ActionController::Base

  def url_options
    { :profile => current_profile }.merge(super)
  end

end

现在,:profile => current_profile 将自动合并到路径/url 参数。

路由示例:

scope ":profile" do
  resources :comments
end

只需写入:

comments_path

如果 current_profile 将 to_param 设置为“lucas”:

/lucas/comments

To set url options for current request use something like this in your controller:

class ApplicationController < ActionController::Base

  def url_options
    { :profile => current_profile }.merge(super)
  end

end

Now, :profile => current_profile will be automerge to path/url parameters.

Example routing:

scope ":profile" do
  resources :comments
end

Just write:

comments_path

and if current_profile has set to_param to 'lucas':

/lucas/comments
嘿哥们儿 2024-11-16 09:47:48

我相信现在首选的方法是告诉路由器处理这个问题:

Rails.application.routes.default_url_options[:foo]= 'bar' 

您可以将此行放在 routes.rb 或初始化程序中。无论您喜欢哪个。如果值根据您的环境而变化,您甚至可以将其放入您的环境配置中。

I believe the preferred method is to now tell the router to handle this:

Rails.application.routes.default_url_options[:foo]= 'bar' 

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.

弥繁 2024-11-16 09:47:48

该 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

救星 2024-11-16 09:47:48

特别是对于 Rails 3,规范的方法是将 default_url_options 方法添加到 ApplicationController 中。

class ApplicationController < ActionController::Base
  def default_url_options
    {
        :host => "corin.example.com",
        :port => "80"  #  Optional. Set nil to force Rails to omit
                       #    the port if for some reason it's being
                       #    included when you don't want it.
    }
  end
end

我只需要自己解决这个问题,所以我知道它是有效的。

这是改编自 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 your ApplicationController.

class ApplicationController < ActionController::Base
  def default_url_options
    {
        :host => "corin.example.com",
        :port => "80"  #  Optional. Set nil to force Rails to omit
                       #    the port if for some reason it's being
                       #    included when you don't want it.
    }
  end
end

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

空‖城人不在 2024-11-16 09:47:48

Rails.application.routes.default_url_options[:host]= 'localhost:3000'

在developemnt.rb / test.rb中,可以更简洁如下:

Rails.application.configure do
  # ... other config ...

  routes.default_url_options[:host] = 'localhost:3000'
end

Rails.application.routes.default_url_options[:host]= 'localhost:3000'

In the developemnt.rb / test.rb, can be more concise as following:

Rails.application.configure do
  # ... other config ...

  routes.default_url_options[:host] = 'localhost:3000'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文