Rails路由错误问题

时间:2013-07-01 17:26:01

标签: 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 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看起来像这样:

match "/:username" => "recipes#index"
scope ':username' do
  resources :recipes
end

我的控制器索引如下:

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

我的控制器显示如下:

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动作就在那里,但在错误中它表示没有。

感谢所有帮助

1 个答案:

答案 0 :(得分:3)

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