Recursive Routes in Rails

Recursive routes in Rails

You can map the initial route (/page) to the controller, setting up "globbing" for all the trailing parameters.

map.connect '/:page/*pages', :controller => 'pages', :action => 'show' 

params[:pages] will now contain an array of the page parameters (matching as many trailing params as you specify in the URL).

Recursively redirecting in Rails 3 Routes

There are many ways to handle this, including additional routes in routes.rb like you've tried, or rewrites at the webserver level such as Apache or Nginx.

One solution I like is the rack-rewrite gem; it keeps routes clean and the rules are plain ruby. It provides a set of methods for configuring rewrites and redirects. For your situation, I would want to use permanent redirects. Though I haven't tested it out myself, something like the following may work for you:

# Gemfile
gem 'rack-rewrite'

# application.rb
config.middleware.use(Rack::Rewrite) do
r301 %r{/merchant|seller/(\w+)}, '/advertiser/$1'
end

Nested routing in Ruby on Rails

You can do that with regular routes and Route Globbing, so for example,

map.connect 'categories/*slugs', :controller => 'categories', :action => 'show_deeply_nested_category'

Then in your controller

def show_deeply_nested_category
do_something = params[:slugs] # contains an array of the path segments
end

However, note that nested resource routing more than one level deep isn't recommended.

Simple Acts_as_tree with nested_resources

A)

If you have a solution for the routing and the only problem with it is that you're concerned about duplicate content issues, you could consider adding <link rel="canonical" href="..."> to the pages generated from those requests. It's not bulletproof though, as Google considers it a suggestion.

Not sure if the route globbers solution would take care of generating the URLs with parent IDs though.

B)

You don't need the parent IDs to perform the routing, correct? You just want to include them in the URLs and route those requests the same as if using the URLs like example.com/chapter1, correct?

If you'd consider a solution that's not purely at the Rails level, what about rewriting the URLs on those requests so that /:parent_id/:parent_id/:id becomes /:id before Rails processes it? That would be easier if there was a static prefix, like /articles/:parent_id/:parent_id/:id.

I imagine you'd need to write some helpers to generate the URLs with parent IDs for linking to those resources.

Duplicate Content

Either way, you'll need to generate URLs that include the parent IDs, so duplicate content issues probably aren't too likely if you only link to those resources using those URLs.

rails helper recursive block strange behaviour

I found solution to explicit use &block as following

def sidebar_link(text,link, color = nil, &block)
recognized = Rails.application.routes.recognize_path(link)
output = ""
content_tag(:li, :class => ( "sticker sticker-color-#{color}" if color) ) do
output << link_to( text, link, :class => ( 'lead' if recognized[:controller] == params[:controller] && recognized[:action] == params[:action]) )
output << capture(&block) if block_given?
raw output
end
end

Recursively including all model subdirectories

this should be helpful

 Dir["#{config.root}/app/models/**/","#{config.root}/lib/**/"]

enjoy! (:

Update:

Excellent question, posting example above i have simply referred to my recent project.

After making some tests, better understanding comes to me and it is great.

The main difference is of course neither in join method of File not config.root / Rails.root

Trailing '/' after '**' makes sense.

First one talks to match only directories when globbing.
Second one talks do it recursively.

In your case this one could be also appropriate

Dir[ Rails.root.join('app', 'models', '**/') ]


Related Topics



Leave a reply



Submit