Rails Routing Error Issue
我是新的Rails开发人员,正在创建一个应用程序,该应用程序会产生以下问题:
1 2 3 | 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>} |
我的routes.rb看起来像这样:
1 2 3 4 | match"/:username" =>"recipes#index" scope ':username' do resources :recipes end |
我的控制器索引如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 |
我的控制器显示如下:
1 2 3 4 5 6 7 8 9 | 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 |
我的索引视图:
1 2 3 4 5 6 7 8 | <% if not @recipes.any? %> No Recipes <% else%> <% @recipes.each do |recipe| %> <%= link_to recipe.name, recipe %> <% end %> |
为什么会这样?显示动作在那里,但是在错误中指出它没有。
感谢所有帮助
1 | <%= link_to recipe.name, recipe_path(username: @user.username, id: recipe.id) %> |