开发者

Creating a rails path for a known action based on current record

开发者 https://www.devze.com 2023-01-28 22:32 出处:网络
As part of my template I show some automatic links which are created in the application_controller. Some of these links will offer a navigation direct from a show page to a corresponding edit page, an

As part of my template I show some automatic links which are created in the application_controller. Some of these links will offer a navigation direct from a show page to a corresponding edit page, and a few similar things.

My current code includes:

nav_links  << {:name=>:edit,:url=>url_for(:action => :edit)}

This works great apart from I have child resources defined in my routes.rb file.

In this case instead of navigating to:

a/1/b/2/edit

It navigates to

b/2/edit

which isn开发者_高级运维't a valid route.

Any ideas on how to accomplish this would be much appreciated. I'm keen to avoid special case where at all possible.


A little confused - why not just use the routes resources define for you. Given that you mentioned 'child resource', I can assume you've declared these resources? If so - you should have a host of routes available to you...

  • Fire off 'rake routes' from the command line
  • Find your route
  • It will most likely take two arguments and look something like this (roughly):

    edit_a_b GET /a/:a_id/b/:id

  • Just use edit_a_b_url(@a, @b)

Hope this helps.

0

精彩评论

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