开发者

Building nested routes for a resource that use acts_as_tree

开发者 https://www.devze.com 2023-03-05 11:15 出处:网络
do you know how to generate dynamically routes such as: ... (/:parent_id(/:parent_id(/:parent_id(/:parent_id))))/:id

do you know how to generate dynamically routes such as:

... (/:parent_id(/:parent_id(/:parent_id(/:parent_id))))/:id

I ask this question because I have a Folder model which acts as tree (it have a parent_id field), and its to_param method return its name which is uniq through the scope of the parent_id. So, each :parent_id and event the :id are the name. Here is an example of path (with 4 sub-folders):

/home/desktop/projects/rails/foobar

...where the first :parent_id (the root) is "home" and the :id is "foobar".

Another example of route could be (with 1 sub-folder):

/home/music

...where, as you can see, params[:parent_id] == 'home' and params[:id] == 'music'.

Is there a clean way to write a beautiful Rails 3 route which handle those kind of possible nested rou开发者_如何学编程tes? Thanks!!


Why don't you just use a route globber and break it up in your controller?

# routes.rb
get "/*folders/:id" => "files#show" 

The *folders section will glob up multiple URL segments.

# files_controller.rb
def show
  folders = params[:folders].split('/') # gives an array of folder names
  # do whatever else necessary
end
0

精彩评论

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