路由问题,rails生成错误的url

JP *_*shy 0 ruby ruby-on-rails

首先,感恩节快乐.

所以我的问题是我的路线,我不清楚为什么id param实际上包含整个对象.Rails给了我这个错误:

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>}
Run Code Online (Sandbox Code Playgroud)

此行发生错误:

<%= link_to recipe.user.username, recipe.user, :class => "user" %>
Run Code Online (Sandbox Code Playgroud)

任何的想法?看起来它应该只生成该id属性的对象.

我的控制器是:

def index
  @recipes = Recipe.search params[:search], :field_weights => { :name => 20, :description => 10 }
end
Run Code Online (Sandbox Code Playgroud)

无法真正看出问题所在.

EmF*_*mFi 5

您是否覆盖了User模型中的to_params或它继承的任何类?

你可以用这个强制id:

 <%= link_to recipe.user.username, user_url(recipe.user.id), :class => "user" %>
Run Code Online (Sandbox Code Playgroud)