JP Silvashy
JP Silvashy

Reputation: 48525

routing issue, rails generates wrong url

First of all, Happy Thanksgiving.

So my issue is with my routes, I'm not clear on why id param actually contains the entire object. Rails gives me this error:

user_url failed to generate from {:action=>"show", :controller=>"users", :id=>#<User id: 19, username: "Dr. Dorothy Buckridge", email: "[email protected]", crypted_password: nil, password_salt: nil, persistence_token: nil, created_at: "2009-11-10 19:38:31", updated_at: "2009-11-10 19:38:31", perishable_token: "", color: nil>}, expected: {:action=>"show", :controller=>"users"}, diff: {:id=>#<User id: 19, username: "Dr. Dorothy Buckridge", email: "[email protected]", crypted_password: nil, password_salt: nil, persistence_token: nil, created_at: "2009-11-10 19:38:31", updated_at: "2009-11-10 19:38:31", perishable_token: "", color: nil>}

The error occurs on this line:

<%= link_to recipe.user.username, recipe.user, :class => "user" %>

Any Idea? It seems like it should only be generating the id of the object for that attribute.

My controller in question is:

def index
  @recipes = Recipe.search params[:search], :field_weights => { :name => 20, :description => 10 }
end

Can't really see what the issue is.

Upvotes: 0

Views: 519

Answers (2)

Marcel Jackwerth
Marcel Jackwerth

Reputation: 54762

This is a wild guess, but I had a similar issue when I typed map.resource :user instead of map.resources :users in the routes.rb

Otherwise validate against EmFi's naswer.

Upvotes: 2

EmFi
EmFi

Reputation: 23450

Did you override to_params in the User model or any class that it inherits?

You can force the id with this:

 <%= link_to recipe.user.username, user_url(recipe.user.id), :class => "user" %>

Upvotes: 5

Related Questions