Micah
Micah

Reputation: 17798

Rails 3 path parameters not part of params

I'm trying to upgrade a Rails 2 app to Rails 3, and I'm really having problems with a route. Here's what I have in the routes.rb file

get 'profile/:login' => 'account#profile', :as => :profile

When I go to http://localhost:3000/profile/MyUsername, it does not correctly add :login to the params hash. See here:

Started GET "/profile/MyUsername?foo=bar" for 127.0.0.1 at Tue Mar 20 21:39:03 -0400 2012
  Processing by AccountController#profile as HTML
  Parameters: {"foo"=>"bar"}

For some reason, :login is not part of the regular params. On a hunch, I inspected the request.env and found this:

action_dispatch.request.path_parameters"=>{:action=>"profile", :controller=>"account", :login=>"MyUsername"}

I'm totally stumped at this point. Am I missing something? Where should I look next to figure out what is going on here?

Update

I started playing with removing gems and this magically worked. I just commented out gems from the Gemfile until I got the absolute minimal set needed to load the homepage. At that point, the params were exactly as expected. Then I added gems back a few at a time in order to find the cause. I added everything back and...it works now. Crazy, but whatever it takes, I guess.

Upvotes: 0

Views: 664

Answers (3)

Erik Peterson
Erik Peterson

Reputation: 54

Something like this should work

match 'profile(/:login)' => "account#profile", :as => :profile

If it does not, there may be something else in your routes file that conflicts. Make sure any match ':controller(/:action(/:id(.:format)))' (or similar "match everything" routes) are at the very bottom of your routes file.

Upvotes: 0

Haris Krajina
Haris Krajina

Reputation: 15286

When I want to use URL params I always use resource(s) when defining the route. That is convention with Rails 3.x so you can try it.

  resources :accounts , :exclude => :all do
    member do
      get  :profile
    end
  end

This should help or any other way of defining resource URL.

Upvotes: 0

jeffersongirao
jeffersongirao

Reputation: 381

Looks like you mixed the syntax for 'match' with 'get'. Please try:

match 'profile/:login' => 'account#profile', :as => :profile, :via => :get

Or

get 'profile/:login', :to => 'account#profile', :as => :profile

in your config/routes.rb

Upvotes: 1

Related Questions