sneglefar sneglefar - 4 months ago 10
Ruby Question

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

I've searched for hours, and I can't seem to find anything that handles my problem here. This might be easy for someone, but it'd be GREATLY appreciated if anyone can help.

I'm creating an app to make a simple CRUD on clients in my database. Index is working fine. And Showing each client with an ID is working great. I see all the details of the client.
Image example of index in app

But with the edit action I get this error in the browser:

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


I don't understand this as I've used the same code in the edit action from the working show action:

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


It appears as if the application is unable to fetch the :id parameter from my browser and place it in the edit form action. The issue might also appear in the update action. I've tried typing in @client = Client.find(1) and it works. It gives me an edit-form with the client_id of 1 in the browser, but of course I want the ID from the browser like how the view action works. So I'm guessing something is wrong with my routing, since the app understands both index and view, but not how to pass on the id to the edit page.

This is what my route file looks like:

Rails.application.routes.draw do

resources :clients

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

end


This is what my edit.html.erb looks like

<%= 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 %>




This is what my rake routes looks like

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


And this is what my clients_controller.rb looks like
`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`


I hope anyone can help! Anything, really, since i'm stuck.
I've tried reading http://guides.rubyonrails.org/routing.html but I cant seem to find out where it all goes wrong.

EDIT: It could also be that I'm linking wrongly to the edit-action. Not sure, but here is the 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>

Answer

You are missing id attribute in your path. Make the following changes please:

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