Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails routing: Giving default values for path helpers

Is there some way to provide a default value to the url/path helpers?

I have an optional scope wrapping around all of my routes:

#config/routes.rb
Foo::Application.routes.draw do

  scope "(:current_brand)", :constraints => { :current_brand => /(foo)|(bar)/ } do
    # ... all other routes go here
  end

end

I want users to be able to access the site using these URLs:

/foo/some-place
/bar/some-place
/some-place

For convenience, I'm setting up a @current_brand in my ApplicationController:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :set_brand

  def set_brand                                                                 
    if params.has_key?(:current_brand)                                          
      @current_brand = Brand.find_by_slug(params[:current_brand])               
    else                                                                        
      @current_brand = Brand.find_by_slug('blah')
    end
  end

 end

So far so good, but now I must modify all *_path and *_url calls to include the :current_brand parameter, even though it is optional. This is really ugly, IMO.

Is there some way I can make the path helpers automagically pick up on @current_brand?

Or perhaps a better way to define the scope in routes.rb?

like image 497
u2622 Avatar asked Mar 29 '12 18:03

u2622


People also ask

How does routing work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

What is the difference between member and collection in Rails route?

Considering the same case, the two terms can be differentiated in a simple way as :member is used when a route has a unique field :id or :slug and :collection is used when there is no unique field required in the route.

What is path helper in Rails?

Searching For Missing Route Values Under the hood, Rails path helpers use ActionDispatch to generate paths that map to routes defined in routes.

What is routes RB in Rails?

rb . The Rails router recognises URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. Let's consider an application to book rooms in different Hotels and take a look at how this works.


2 Answers

I think you will want to do something like this:

class ApplicationController < ActionController::Base

  def url_options
    { :current_brand => @current_brand }.merge(super)
  end

end

This method is called automatically every time url is constructed and it's result is merged into the parameters.

For more info on this, look at: default_url_options and rails 3

like image 136
CMW Avatar answered Oct 15 '22 09:10

CMW


In addition to CMW's answer, to get it to work with rspec, I added this hack in spec/support/default_url_options.rb

ActionDispatch::Routing::RouteSet.class_eval do
  undef_method :default_url_options
  def default_url_options(options={})
    { :current_brand => default_brand }
  end
end
like image 27
u2622 Avatar answered Oct 15 '22 09:10

u2622