开发者

How can I access named routes in a Rails sweeper?

开发者 https://www.devze.com 2023-03-07 07:26 出处:网络
I have a sweeper that reheats cache by opening urls in forked processes. It\'s easy enough to hard-code the host for those urls, but I want it to change dynamically, based on environment.

I have a sweeper that reheats cache by opening urls in forked processes. It's easy enough to hard-code the host for those urls, but I want it to change dynamically, based on environment.

I know I can set global/environmental variables for this, but I'd really like to use named routes. Is there any way to do this?

(I should clarify that I've tried just putting 开发者_JAVA技巧the named routes in there but they don't resolve at runtime.)


The closest I found is:

Rails.application.routes.named_routes.get(:user).defaults

Which retrieves:

{:action=>"show", :controller=>"users"}

But you should merge it with your params:

Rails.application.routes.named_routes.get(:user).defaults.merge({:id => 1})

Result:

{:action=>"show", :controller=>"users", :id=>1}

Edit:

Simply add

include Rails.application.routes.url_helpers 

to your class


I fixed this in the current Rails 3.2 beta:

https://github.com/rails/rails/pull/3876

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号