我已经搜索了几个小时,我似乎找不到任何可以解决我问题的方法。这对某些人来说可能很容易,但如果有人可以提供帮助,我们将非常感激。
我正在创建一个应用程序,以便在我的数据库中对客户端进行简单的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
我不明白这一点,因为我在工作show动作的编辑操作中使用了相同的代码:
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&lt; 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 :(得分:2)
您的路径中缺少id属性。请进行以下更改:
<%= link_to("Edit", {:action => 'edit', :id => client.id}, :class => 'action edit') %>
答案 1 :(得分:1)
您需要在编辑链接中提供id
<%= link_to("Edit", {:action => 'edit', :id => client.id }, :class => 'action edit') %>