Rails错误:无法找到带有' id' = index的节点?

时间:2016-03-30 17:41:58

标签: ruby-on-rails

我生成一个节点来显示节点名称,但是错误。这是鳕鱼。

controllers / 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

rails s 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"}

我该怎么办?怎么调试?谢谢告诉我。

1 个答案:

答案 0 :(得分:1)

您无需点击http://localhost:3000/nodes/index即可在Rails控制器中调用index操作。您所要做的就是转到http://localhost:3000/nodes,它会点击您控制器中的index操作。

http://localhost:3000/nodes/something之类的任何内容都会引导您进行show操作,而something将被视为id以查找特定节点。由于您正在点击:http://localhost:3000/nodes/index,因此Rails将index作为id来查找特定节点。

坚持使用Rails约定和REST原则,让Rails的魔力处理事情,并将以下路由强加于特定操作:

GET: http://localhost:3000/nodes ----> Nodes#index
GET: http://localhost:3000/nodes/something ------> Nodes#show