2016-08-01 65 views
-1

我搜索了几个小时,我似乎无法找到任何处理我的问题的东西。对某人来说这可能很容易,但是如果有人能够帮助,我们将非常感谢。编辑问题:用'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看起来像 `类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> 
+0

没有'Client'与给定的'id'。您可以使用'客户端来检查客户端是否存在于您的控制台中。是否存在?(id_of_the_client_you_are_searching)' – Emu

+0

但是id与我的客户确实存在?我看到他们在我看来,他们被传递到查看表单 – sneglefar

+0

您用来访问您的网页的网址是什么? – Geoffroy

回答

2

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

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

谢谢!事后看来,应该注意到我自己。 – sneglefar

1

您需要提供id您的编辑链接

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

哇。多么简单。这是小事。谢谢你们的努力! – sneglefar

+0

它应该是client而不是@client。 –

+0

真的!标记为正确的答案,因此正确的语法!感谢你们虽然 – sneglefar