with_options and Rails 3

Hey all,

Rails guide doesnt cover with_options in Rails 3:

http://guides.rubyonrails.org/routing.html

The book The Rails 3 Way makes no reference to with_options except for
one page briefly.

And I cannot find a decent tutorial to cover what I am trying to do.

I have this:

map.with_options :name_prefix => "dashboard_", :path_prefix =>
"dashboard", :controller => "dashboard" do |dashboard|
    dashboard.sidebar ".:format"
    dashboard.charts ".:format"
    dashboard.action_items ".:format"
    dashboard.performance ".:format"
    dashboard.site_menu ".:format"
    dashboard.quick_links ".:format"
    dashboard.perf_randomizations ".:format"
  end

Basically all of these methods "sidebar", "charts", etc are all methods
of the dashboard controller. I want them all to have a path helper of
dashboard_#{action}_url, where action is the action (e.g.
dashboard_sidebar_url). I want them all to be able to respond to both
html format and json. However, I dont know how to do this in Rails 3.

The above code raises exception:

config/routes.rb:7:in `block in <top (required)>': undefined local
variable or method `map' for
#<ActionDispatch::Routing::Mapper:0x00000102ca3570> (NameError)

Some stackoverflow posts show similar situations but not specifically
what I am trying to do here.

thanks for response

I tried this:

scope :path => '/dashboard', :name_prefix => "dashboard_", :path_prefix
=> "dashboard", :controller => :dashboard do
      match '/sidebar.:format' => :sidebar
      match '/charts.:format' => :charts
      match '/action_items.:format' => :action_items
      match '/performance.:format' => :performance
  end

not sure yet if it is going to give me dashboard_sidebar_url, for
example, as a route helper.

I tried a named route and I get this:

undefined local variable or method `dashboard_sidebar_path'

any idea?

John Merlino wrote in post #1019442:

Hey all,

Rails guide doesnt cover with_options in Rails 3:

http://guides.rubyonrails.org/routing.html

The book The Rails 3 Way makes no reference to with_options except for
one page briefly.

The docs say with_options is still part of rails 3.0.9:

http://apidock.com/rails/Object/with_options

...but like a lot of rails stuff that method probably just serves to
obfuscate your code--even though it's terser.

And I cannot find a decent tutorial to cover what I am trying to do.

I have this:

map.with_options

Whoa. In rails 3, you write routes in a block that looks like this:

  TestApp::Application.routes.draw do

    ...

  end

not like rails 2:

  ActionController::Routing::Routes.draw do |map|

    map.do_something

  end

If you write this:

  TestApp::Application.routes.draw do

    map.foo.bar

  end

...then ruby doesn't know what map is.

:name_prefix => "dashboard_", :path_prefix =>
"dashboard", :controller => "dashboard" do |dashboard|
    dashboard.sidebar ".:format"
    dashboard.charts ".:format"
    dashboard.action_items ".:format"
    dashboard.performance ".:format"
    dashboard.site_menu ".:format"
    dashboard.quick_links ".:format"
    dashboard.perf_randomizations ".:format"
  end

Basically all of these methods "sidebar", "charts", etc are all methods
of the dashboard controller. I want them all to have a path helper of
dashboard_#{action}_url, where action is the action (e.g.
dashboard_sidebar_url). I want them all to be able to respond to both
html format and json. However, I dont know how to do this in Rails 3.

See if this works:

match 'some/url' => "dashboard#sidebar", :as => "dashboard_sidebar"
match 'another/url' => "dashboard#charts", :as => "dashboard_charts"

I havent tested it yet but thanks for response

So I tried using the :as option for the named route so I could have
dashboard_sidebar_path helpers.

scope :path => '/dashboard', :controller => :dashboard do
      match '/sidebar.:format' => :sidebar, :as => 'dashboard_sidebar'
      match '/charts.:format' => :charts
      match '/action_items.:format' => :action_items
      match '/performance.:format' => :performance
  end

However, strangely enough, it reports this error when using the
dashboard_sidebar_path helper:

No route matches {:controller=>"dashboard", :action=>"sidebar"}

As you can see the controller is defined with :controller in the
scope, so I dont know why the error.

thanks for response

Yeah, in old Rails (2.x) the block created a map variable (you could observe “do |map|” in the first line) but in Rails 3 the API has changed and this variable is not created anymore, hence you can´t use it.

Rodrigo Vieira
Programmer

+55 (81) 98935478

http://www.rodrigoalvesvieira.com

https://github.com/rodrigoalvesvieira

http://twitter.com/#!/rodrigoalvieira

thanks for response

I see some other strange stuff going on.

for example, I have something like this:

  resources :players do
    member do
      get :reject
    end
    resources :saleries
  end

And I use a link_to helper to try to access the reject_player_path in
my dashboard show haml file and I get an error:

No route matches {:action=>"reject", :controller=>"players"}

And I try to even just try a player_path helper, which should exist
for show (when using link_to in other words get requests) and update
(when using form_for in other words post requests), yet I get this
error:

No route matches {:action=>"show", :controller=>"players"}

But obviously, these exceptions are wrong, as when I run rake routes,
these matches indeed do exist:

            root /
(.:format)
{:controller=>"dashboard", :action=>"show"}
      team_players GET /teams/:team_id/
players(.:format)
{:action=>"index", :controller=>"players"}
                   POST /teams/:team_id/
players(.:format)
{:action=>"create", :controller=>"players"}
   new_team_player GET /teams/:team_id/players/
new(.:format) {:action=>"new", :controller=>"players"}
  edit_team_player GET /teams/:team_id/players/:id/
edit(.:format) {:action=>"edit", :controller=>"players"}
       team_player GET /teams/:team_id/
players/:id(.:format)
{:action=>"show", :controller=>"players"}
                   PUT /teams/:team_id/
players/:id(.:format)
{:action=>"update", :controller=>"players"}
                   DELETE /teams/:team_id/
players/:id(.:format)
{:action=>"destroy", :controller=>"players"}
             teams GET /
teams(.:format)
{:action=>"index", :controller=>"teams"}
                   POST /
teams(.:format)
{:action=>"create", :controller=>"teams"}
          new_team GET /teams/
new(.:format)
{:action=>"new", :controller=>"teams"}
         edit_team GET /teams/:id/
edit(.:format)
{:action=>"edit", :controller=>"teams"}
              team GET /
teams/:id(.:format)
{:action=>"show", :controller=>"teams"}
                   PUT /
teams/:id(.:format)
{:action=>"update", :controller=>"teams"}
                   DELETE /
teams/:id(.:format)
{:action=>"destroy", :controller=>"teams"}
     reject_player GET /players/:id/
reject(.:format)
{:action=>"reject", :controller=>"players"}
   player_saleries GET /players/:player_id/
saleries(.:format)
{:action=>"index", :controller=>"saleries"}
                   POST /players/:player_id/
saleries(.:format)
{:action=>"create", :controller=>"saleries"}
new_player_salery GET /players/:player_id/saleries/
new(.:format) {:action=>"new", :controller=>"saleries"}
edit_player_salery GET /players/:player_id/saleries/:id/
edit(.:format) {:action=>"edit", :controller=>"saleries"}
     player_salery GET /players/:player_id/
saleries/:id(.:format) {:action=>"show", :controller=>"saleries"}
                   PUT /players/:player_id/
saleries/:id(.:format)
{:action=>"update", :controller=>"saleries"}
                   DELETE /players/:player_id/
saleries/:id(.:format)
{:action=>"destroy", :controller=>"saleries"}
           players GET /
players(.:format)
{:action=>"index", :controller=>"players"}
                   POST /
players(.:format)
{:action=>"create", :controller=>"players"}
        new_player GET /players/
new(.:format)
{:action=>"new", :controller=>"players"}
       edit_player GET /players/:id/
edit(.:format)
{:action=>"edit", :controller=>"players"}
            player GET /
players/:id(.:format)
{:action=>"show", :controller=>"players"}
                   PUT /
players/:id(.:format)
{:action=>"update", :controller=>"players"}
                   DELETE /
players/:id(.:format)
{:action=>"destroy", :controller=>"players"}
          saleries GET /
saleries(.:format)
{:action=>"index", :controller=>"saleries"}
                   POST /
saleries(.:format)
{:action=>"create", :controller=>"saleries"}
        new_salery GET /saleries/
new(.:format)
{:action=>"new", :controller=>"saleries"}
       edit_salery GET /saleries/:id/
edit(.:format)
{:action=>"edit", :controller=>"saleries"}
            salery GET /
saleries/:id(.:format)
{:action=>"show", :controller=>"saleries"}
                   PUT /
saleries/:id(.:format)
{:action=>"update", :controller=>"saleries"}
                   DELETE /
saleries/:id(.:format)
{:action=>"destroy", :controller=>"saleries"}
dashboard_sidebar /dashboard/
sidebar.:format
{:controller=>"dashboard", :action=>"sidebar"}
                          /dashboard/
charts.:format
{:controller=>"dashboard", :action=>"charts"}
                          /dashboard/
action_items.:format
{:controller=>"dashboard", :action=>"action_items"}
                          /dashboard/
performance.:format
{:controller=>"dashboard", :action=>"performance"}

So it seems to be contradicting.

bump

The reason why player path wasnt working was because it required an
instance of player: player_path(@player). But I am still stuck as to
why dashboard_sidebar_path isn't working when the route clearly shows
up when running rake routes.