编辑问题:找不到 'id'= 的客户

Edit issue: Couldn't find Client with 'id'=

我已经搜索了几个小时,似乎在这里找不到任何可以解决我的问题的东西。这对某些人来说可能很容易,但如果有人能提供帮助,我们将不胜感激。

我正在创建一个应用程序来对我的数据库中的客户端进行简单的 CRUD。索引工作正常。向每个客户显示一个 ID 效果很好。我看到了客户的所有细节。 Image example of index in app

但是在编辑操作中我在浏览器中得到这个错误:

ActiveRecord::RecordNotFound in ClientsController#edit 
Couldn't find Client with 'id'=

Extracted source (around line #30):

29 def edit
30   @client = Client.find(params[:id])
31 end
32
33 def update

我不明白这一点,因为我在工作表演动作的编辑动作中使用了相同的代码:

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

应用程序似乎无法从我的浏览器获取 :id 参数并将其放置在编辑表单操作中。该问题也可能出现在更新操作中。我试过输入 @client = Client.find(1) 并且有效。它在浏览器中为我提供了一个 client_id 为 1 的编辑表单,但我当然想要来自浏览器的 ID,就像查看操作的工作方式一样。所以我猜我的路由有问题,因为应用程序理解索引和视图,但不知道如何将 id 传递到编辑页面。

这是我的路由文件的样子:

Rails.application.routes.draw do

  resources :clients

  match ':controller(/:action(:/id))', :via => [:get, :post]

end

这就是我的 edit.html.erb 的样子

<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>

<div class="clients edit">
<h2>Update client</h2>

<%= form_for(:client, :url => {:action => 'update', :id => @client.id}) do |f| %>

    <table summary="Client form fields">
        <tr>
            <th>Name</th>
            <td><%= f.text_field(:name) %></td>
        </tr>
        <tr>
            <th>Visible</th>
            <td><%= f.text_field(:visible) %></td>
        </tr>
        <tr>
            <th>Category</th>
            <td><%= f.text_field(:category_id) %></td>
        </tr>
        <tr>
            <th>Short description</th>
            <td><%= f.text_field(:shortdescription) %></td>
        </tr>
        <tr>
            <th>Long description</th>
            <td><%= f.text_field(:longdescription) %></td>
        </tr>
        <tr>
            <th>Phonenumber</th>
            <td><%= f.text_field(:phonenumber) %></td>
        </tr>
        <tr>
            <th>Email</th>
            <td><%= f.text_field(:email) %></td>
        </tr>
        <tr>
            <th>Country</th>
            <td><%= f.text_field(:country) %></td>
        </tr>
        <tr>
            <th>Lattitude</th>
            <td><%= f.text_field(:lattitude) %></td>
        </tr>
        <tr>
            <th>Longtitude</th>
            <td><%= f.text_field(:longtitude) %></td>
        </tr>
    </table>

    <div class="form-buttons">
        <%= submit_tag("Update Client") %>
    </div>

<% end %>

这是我的佣金路线的样子

     Prefix Verb     URI Pattern                            Controller#Action
    clients GET      /clients(.:format)                     clients#index
        POST     /clients(.:format)                     clients#create
 new_client GET      /clients/new(.:format)                 clients#new
edit_client GET      /clients/:id/edit(.:format)            clients#edit
 client GET      /clients/:id(.:format)                 clients#show
        PATCH    /clients/:id(.:format)                 clients#update
        PUT      /clients/:id(.:format)                 clients#update
        DELETE   /clients/:id(.:format)                 clients#destroy
   root GET      /                                      clients#index

这就是我的 clients_controller.rb 的样子 `class ClientsController < ApplicationController

def index
    @clients = Client.order("name ASC")
end

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

def new
    @client = Client.new({:country => 'DK'})
end

def create
    @client = Client.new(client_params)
    if @client.save
        redirect_to(:action => 'index')
    else
        render('new')
    end
end

def edit
    @client = Client.find(params[:id])
end

def update
    @client = Client.find(params[:id])
    if @client.update_attributes(client_params)
        redirect_to(:action => 'show', :id => @client.id)
    else
        render('edit')
    end
end


def delete
end

private

    def client_params

        params.require(:client).permit(:name, :visible, :category_id, :shortdescription, :longdescription, :phonenumber, :email, :country, :lattitude, :longtitude)
    end
end`

希望大家帮帮忙!真的,因为我被困住了。 我试过阅读 http://guides.rubyonrails.org/routing.html 但我似乎无法找出哪里出了问题。

编辑: 也可能是我错误地链接到编辑操作。不确定,但这是 index.html.erb

<div class="clients index">
<h2>Clients</h2>

<%= link_to("Add New Client", {:action => 'new'}, :class => 'action new') %>

<table class="listing" summary="Clients list">
    <tr class="header">

        <th>id</th>
        <th>Name</th>
        <th>Category</th>
        <th>Visible</th>
        <th>Actions</th>
    </tr>
    <% @clients.each do |client| %>
    <tr>
        <td><%= client.id %></td>
        <td class="center"><%= client.name %></td>
        <td class="center"><%= client.category_id %></td>
        <td class="center"><%= client.visible ? 'Yes' : 'No' %></td>
        <td class="actions">
            <%= link_to("Show", {:action => 'show', :id => client.id}, :class => 'action show') %>
            <%= link_to("Edit", {:action => 'edit'}, :class => 'action edit') %>
            <%= link_to("Delete", '#', :class => 'action delete') %>
        </td>
        <td></td>
    </tr>
    <% end %>
</table>
</div> 

您需要为您的编辑提供 id link

<%= link_to("Edit", {:action => 'edit', :id => client.id }, :class => 'action edit') %>

您的路径中缺少 id 属性。请进行以下更改:

<%= link_to("Edit", {:action => 'edit', :id => client.id}, :class => 'action edit') %>