Ruby on rails Rails路由错误问题

Ruby on rails Rails路由错误问题,ruby-on-rails,ruby,ruby-on-rails-3,routing,Ruby On Rails,Ruby,Ruby On Rails 3,Routing,我是一名新的rails开发人员,正在创建一个应用程序,该应用程序会产生以下问题: Routing Error No route matches {:action=>"show", :controller=>"recipes", :username=>#<Recipe id: 42, name: "Curried Chicken Sandwich", yield: "4 Servings", description: "Burgers aren't the only gr

我是一名新的rails开发人员,正在创建一个应用程序,该应用程序会产生以下问题:

Routing Error

No route matches {:action=>"show", :controller=>"recipes", :username=>#<Recipe id: 42, name: "Curried Chicken Sandwich", yield: "4 Servings", description: "Burgers aren't the only grilled things we want to e...", created_at: "2013-06-27 18:49:42", updated_at: "2013-06-27 18:49:42", duration: "", author: nil, url: nil, user: 3, image: "curried-chicken-sandwich-646.jpg", user_recipe_id: nil>}
我的控制器索引如下所示:

def index
 if params[:tag]
  @recipes = Recipe.tagged_with(params[:tag]).where(:user => current_user.id).paginate(:page => params[:page], :per_page => 45)
 else
  @user = User.find_by_username params[:username]
  @search = Recipe.search do |query|
    query.fulltext params[:search]
    query.with(:user, @user.id)
    query.paginate(:page => params[:page] || 1, :per_page => 20)
 end
  @recipes = @search.results
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @recipes }
  end
 end
end
我的控制器显示如下:

match "/:username" => "recipes#index"
scope ':username' do
  resources :recipes
end
def show
   @user = User.find_by_username params[:username]
   @recipe = Recipe.where(:user_recipe_id => params[:id])

   respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @recipe }
   end
end
我的索引视图:

<% if not @recipes.any? %>
No Recipes
<% else%>

<% @recipes.each do |recipe| %>

    <h3><%= link_to recipe.name, recipe %></h3>
<% end %>

没有食谱
为什么会发生这种情况?show动作在那里,但是在错误中它说它没有

感谢所有帮助


<%= link_to recipe.name, recipe_path(username: @user.username, id: recipe.id) %>



错误可能是由视图代码引发的。你能展示一下吗?我不确定你能不能这样使用路由作用域。这个错误是在哪里触发的?@MarekLipka,好的,我刚刚更新了我的question@Lichtamberg,错误显示在我的索引页上。错误可能是由视图代码引发的。你能展示一下吗?我不确定你能不能这样使用路由作用域。这个错误是在哪里触发的?@MarekLipka,好的,我刚刚更新了我的question@Lichtamberg,错误会显示在“我的索引页”上修复拼写,您可能还需要传递实际用户名,而不是用户对象。但这基本上就是你想要的。@Lichtamberg,谢谢你的回复,但是错误仍然出现了。@the_u你试过@GoGoCarl提到的吗?将
@user
替换为
@user.username
,并确保正确拼写
配方。“它仍然不起作用吗?”他们说,我刚试过GoGoCarl的建议,但还是出现了同样的错误。如果用户没有与他/她相关的食谱,那么就不会出现错误。我继续并建议对答案进行编辑,只是为了澄清评论。修复您的拼写,您可能还需要传递实际用户名,而不是用户对象。但这基本上就是你想要的。@Lichtamberg,谢谢你的回复,但是错误仍然出现了。@the_u你试过@GoGoCarl提到的吗?将
@user
替换为
@user.username
,并确保正确拼写
配方。“它仍然不起作用吗?”他们说,我刚试过GoGoCarl的建议,但还是出现了同样的错误。如果用户没有与他/她相关的食谱,那么就不会出现错误。我继续对答案进行编辑,只是为了澄清评论。