ActiveRecord :: RecordNotFound无法找到id = index的配方

时间:2014-06-05 22:07:23

标签: ruby-on-rails activerecord routing params

我正在处理Rails应用程序,但遇到以下错误消息。

ActiveRecord::RecordNotFound in RecipesController#show

Couldn't find recipe with id=index

Extracted source (around line #8)

def show
  @recipe = Recipe.find(params[:id]) # This line is highlighted in pink
end

我认为这与我的路线有关。我的索引页面的url是localhost:3000 / games / index(我在我的食谱控制器上调用了索引操作。我通过资源创建了这个动作:recipe创建了7条休息路由,包括索引。)但是,当我访问时它,ROR认为我通过show route访问id为index的游戏。事实是我想将我的索引页面设置为localhost:3000 / recipes。我最后不想要索引。

这是我的RecipesController

class RecipesController < ApplicationController

  def index
    @recipe = Recipe.all
  end

  def show
    @recipe = recipe.find(params[:id])
  end

  def new
    @recipe = Recipe.new
  end

  def create
    @recipe = Recipe.new(params[:id])
    if @recipe.save
      redirect_to @recipe
    else
      render 'new'
    end
  end

  def edit
  end

  def update
  end

  def destroy
  end

end

正如您所看到的,我定义了7条常见的RESTful路由并完成了4条路线。

这是我的routes.rb文件

Recipes::Application.routes.draw do
  resources :recipes
end

通过声明资源:食谱,我创建了7条路线,而不必在多行上声明它们。

以下是佣金路线的结果

Prefix     Verb   URI Pattern               Controller#Action
recipes      GET    /recipes(.:format)          recipes#index
           POST   /recipes(.:format)          recipes#create
new_recipe   GET    /recipes/new(.:format)      recipes#new
edit_recipe  GET    /recipes/:id/edit(.:format) recipes#edit
recipe      GET    /recipes/:id(.:format)      recipes#show
           PATCH  /recipes/:id(.:format)      recipes#update
           PUT    /recipes/:id(.:format)      recipes#update
           DELETE /recipes/:id(.:format)      recipes#destroy

这是我在index / views / recipes目录中的index.html.erb文件

<h1>Here is a list of recipes</h1>
  <table>
    <thead>
  <tr>
    <th>Recipe</th>
    <th>Cook Time</th>
    <th>Prep Time</th>
    <th>Difficulty</th>
  </tr>
</thead>
    <tbody> 
  <% @recipes.each do |recipe| %>
    <tr>
      <td><%= recipe.recipe %></td>
      <td><%= recipe.cooktime %></td>
      <td><%= recipe.preptime %></td>
      <td><%= recipe.difficulty %></td>
    </tr>
  <% end %>
</tbody>
  </table>

这是我的迁移文件

class CreateRecipes < ActiveRecord::Migration
  def change
    create_table :recipes do |t|
      t.string :recipe
      t.string :cooktime
      t.string :preptime
      t.integer :difficulty

      t.timestamps
    end
  end
end

以下是我的application.html.erb文件,用作每个网页的模板

<!DOCTYPE html>
  <html>
  <head>
    <title>Recipes</title>
      <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
      <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>
  </head>
  <body>

    <%= yield %>

    <%= link_to "Home", recipes_path %>
    <%= link_to "Edit", edit_recipe_path(:id) %>
    <%= link_to "New", new_recupe_path %>
    <%= link_to "Delete" %>
  </body>
</html>

我尝试通过rails控制台创建ActiveRecord对象并将其保存。所以ActiveRecord对象确实存在ID。我不知道我在哪里出错了。

如果您需要更多信息,请告诉我们。

我感谢任何帮助。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

是什么给你这个错误?我可以想象会叫/games/index。基本上索引被解析为/games/:id路由的参数。

编辑:

这是因为布局模板中的<%= link_to "Edit", edit_game_path(:id) %>调用。这是错位的。如果您正在查看/games,则无法进行编辑,因为不存在:id

相关问题