开发者

Using Params in Rails Routes for generating url

开发者 https://www.devze.com 2023-04-04 09:44 出处:网络
I have a scoped route: scope :path => \":db\", :defaults => {:db => \"default\"} do resources :boxes

I have a scoped route:

 scope :path => ":db", :defaults => {:db => "default"} do
   resources :boxes
 end

That generates

    boxes GET    /:db/boxes(.:format)          {:db=>"default", :action=>"index", :controller=>"boxes"}
         POST   /:db/boxes(.:format)          {:db=>"default", :action=>"create", :controller=>"boxes"}
 new_box GET    /:db/boxes/new(.:format)      {:db=>"default", :action=>"new", :controller=>"boxes"}
edit_box GET    /:db/boxes/:id/edit(.:format) {:db=&g开发者_如何学Ct;"default", :action=>"edit", :controller=>"boxes"}
     box GET    /:db/boxes/:id(.:format)      {:db=>"default", :action=>"show", :controller=>"boxes"}
         PUT    /:db/boxes/:id(.:format)      {:db=>"default", :action=>"update", :controller=>"boxes"}
         DELETE /:db/boxes/:id(.:format)      {:db=>"default", :action=>"destroy", :controller=>"boxes"}

I would like to be able to call boxes_url (for example) and have it infer that the value for :db should be inferred from the current request.

Example:

A request is made for "/lax/boxes". In the template, I call box_url(@box), and it generates the url "/lax/boxes/1" (where @box.id = 1).

The only solution I currently see is to always include params[:db] in url, calling box_url(params[:db],@box).

Does anyone know anyway to get the behavior I've described?


This (in a helper file) should work:

def box_url(box)
    super(params[:db], box)
end

Unfortunately, you'll have to repeat this for each url/path you want to override.

0

精彩评论

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