2016-03-30 10 views
0

ノード名を表示するノードを生成しますが、エラーです。ここにコモがあります。Railsエラー: 'id' = indexのノードが見つかりませんでしたか?

コントローラ/ nodes_controller.rb(デフォルト)

class NodesController < ApplicationController 
    before_action :set_node, only: [:show, :edit, :update, :destroy] 


    def index 
    @nodes = Node.all 
    end 

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

    def new 
    @node = Node.new 
    end 

    def edit 
    end 

    def create 
    @list = Node.new(node_params) 

    respond_to do |format| 
     if @node.save 
     format.html { redirect_to @node, notice: 'Node was successfully created.' } 
     format.json { render :show, status: :created, location: @node } 
     else 
     format.html { render :new } 
     format.json { render json: @list.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /lists/1 
    # PATCH/PUT /lists/1.json 
    def update 
    respond_to do |format| 
     if @node.update(node_params) 
     format.html { redirect_to @node, notice: 'Node was successfully updated.' } 
     format.json { render :show, status: :ok, location: @node } 
     else 
     format.html { render :edit } 
     format.json { render json: @node.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /lists/1 
    # DELETE /lists/1.json 
    def destroy 
    @node.destroy 
    respond_to do |format| 
     format.html { redirect_to nodes_url, notice: 'Node was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_node 
     @node = Node.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def node_params 
     params.require(:node).permit(:name, :summary) 
    end 
end 

モデル/ node.rb

class Node < ActiveRecord::Base 
    has_many :lists 
end 

ビュー/ノード/ index.html.erb

<div class="nodes_list"> 
    nodes: <br> <br> 
    <% nodes.each do |node| %> 
     <button class="secondary hollow button tiny"> 
     <% if list.node %> 
      <%= link_to node.name, node %> 
     <% end %> 
     </button> 
    <% end %> 
</div> 

routes.rbを

Rails.application.routes.draw do 
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 
    resources :lists, only: [:index, :show, :new, :create] 
    resources :nodes, only: [:show, :index, :create, :new] 

    root 'lists#index' 
end 
本の

レールのhttp://localhost:3000/nodes/index

ActiveRecord::RecordNotFound in NodesController#show 
Couldn't find Node with 'id'=index 

Extracted source (around line #61):  
    # Use callbacks to share common setup or constraints between actions. 
60 def set_node 
61 @node = Node.find(params[:id]) 
62 end 

Request 

Parameters: 

{"id"=>"index"} 

私は何をすべき?デバッグするには?ありがとうございます。

+2

あなたが間違ったルートになるだろう。インデックスは '/ nodes /'にあり、ルート '/ nodes/index'はIDの' 'index 'でSHOWをレンダリングしようとしています – tpbowden

+1

' http:// localhost:3000/nodes/indexのようなURLを手動で入力していますか? '? – Pavan

+0

ああ、私の神。知っている。私はとても不注意です。今はすべてがOKです。 – dongdongxiao

答えて

1

indexアクションをRailsコントローラで呼び出すには、http://localhost:3000/nodes/indexを押す必要はありません。 http://localhost:3000/nodesに行くだけで、コントローラのindexアクションに当たることになります。 http://localhost:3000/nodes/somethingよう

ものはshowアクションが表示されます、そしてsomethingは、特定のノードを見つけるためにidとさせていただきます。 http://localhost:3000/nodes/indexなので、Railsはindexidとして特定のノードを見つけます。

Railsの規則に固執する、との原則をREST、とRailsの魔法は、物事の世話を聞かせて、それが特定のアクションに次のルートをスティック:

GET: http://localhost:3000/nodes ----> Nodes#index 
GET: http://localhost:3000/nodes/something ------> Nodes#show 
関連する問題