开发者

routing issue, rails generates wrong url

开发者 https://www.devze.com 2022-12-12 20:23 出处:网络
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:

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: "kyra@hansenstehr.ca", 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: "kyra@hansenstehr.ca", 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.


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" %>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消