I've viewed several different similar SO posts and tried the solutions, but none are working for me, and I've been stuck on this for a few days now. I think there may be something else wrong with my application causing this problem.
Problem: I need to make a link to edit a contact's address. I know the route requires both IDs, but I cannot figure out why the variables I am providing fail to give the address id like they do in the destroy method link. It may be something wrong with this piece: <%= link_to 'Edit Address', edit_contact_address_path(address.contact, address) %> or maybe something in one of my controller's edit methods. I can't figure it out. I appreciate any help. Apologies for any foolish mistakes, I'm very new to this.
Error:
Started GET "/contacts/16/" for ::1 at 2020-09-20 14:02:46 +0200
Processing by ContactsController#show as HTML
Parameters: {"id"=>"16"}
Contact Load (13.8ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = $1 LIMIT $2 [["id", 16], ["LIMIT", 1]]
↳ app/controllers/contacts_controller.rb:8:in `show'
Rendering contacts/show.html.erb within layouts/application
Address Load (6.3ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."contact_id" = $1 [["contact_id", 16]]
↳ app/views/contacts/show.html.erb:21
Rendered contacts/show.html.erb within layouts/application (Duration: 11.3ms | Allocations: 2127)
Completed 500 Internal Server Error in 33ms (ActiveRecord: 20.4ms | Allocations: 3488)
ActionView::Template::Error (No route matches {:action=>"edit", :contact_id=>"16", :controller=>"addresses", :id=>nil}, missing required keys: [:id]):
28: <td><%= address.zip %></td>
29: <td><%= address.state %></td>
30: <td><%= address.country %></td>
31: <td><%= link_to 'Edit Address', edit_contact_address_path(address.contact, address) %></td>
32: <td><%= link_to 'Destroy Address', [address.contact, address],
33: method: :delete,
34: data: { confirm: 'Are you sure?' } %></td>
app/views/contacts/show.html.erb:31
app/views/contacts/show.html.erb:21
routes.rb
Rails.application.routes.draw do
resources :contacts do
resources :addresses
end
root to: 'welcome#index'
end
models/address.rb
class Address < ApplicationRecord
validates :street, :town, :zip, :country, presence: true
belongs_to :contact
end
models/contact.rb
class Contact < ApplicationRecord
has_many :addresses, dependent: :destroy
validates :first_name, presence: true
validates :last_name, presence: true
end
[...excerpt...]
<% if @contact.addresses.any? %>
<h2>Addresses</h2>
<% @contact.addresses.each do |address| %>
<% debug address.id %>
<table>
<tr>
<td><%= address.id %></td>
<td><%= address.street %></td>
<td><%= address.town %></td>
<td><%= address.zip %></td>
<td><%= address.state %></td>
<td><%= address.country %></td>
<td><%= link_to 'Edit Address', edit_contact_address_path(address.contact, address) %></td>
<td><%= link_to 'Destroy Address', [address.contact, address],
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>
controllers/contacts_controller.rb
class ContactsController < ApplicationController
[...excerpt...]
def show
@contact = Contact.find(params[:id])
@address = @contact.addresses.new
end
controllers/addresses_controller.rb
class AddressesController < ApplicationController
[...excerpt...]
def new
@contact = Contact.find(params[:contact_id])
@address = Address.new
end
def edit
@contact = Contact.find(params[:contact_id])
@address = @contact.addresses.find(params[:id])
end
rake routes:
contact_addresses GET /contacts/:contact_id/addresses(.:format) addresses#index
POST /contacts/:contact_id/addresses(.:format) addresses#create
new_contact_address GET /contacts/:contact_id/addresses/new(.:format) addresses#new
edit_contact_address GET /contacts/:contact_id/addresses/:id/edit(.:format) addresses#edit
contact_address GET /contacts/:contact_id/addresses/:id(.:format) addresses#show
PATCH /contacts/:contact_id/addresses/:id(.:format) addresses#update
PUT /contacts/:contact_id/addresses/:id(.:format) addresses#update
DELETE /contacts/:contact_id/addresses/:id(.:format) addresses#destroy
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format)
The error starts here:
def show
@contact = Contact.find(params[:id])
@address = @contact.addresses.new
end
Calling @contact.addresses.new
creates a new Address
model tied to your @contact
. The brand new Address
does not yet have an ID. You are doing this even if the @contact
already has addresses saved. So, when you start your iteration:
<% @contact.addresses.each do |address| %>
Once it hits that unsaved Address
, it will create a link with a nil
ID; clicking that will not work.
Removing the unused @address = @contact.addresses.new
line will make the page work, though you won't have any addresses in the table yet. If you're trying to test, you can always add a link to that contact page to new_contact_address_path(@contact)
to make a new one!
Update: To continue using @address
for a form on the page, you can create it from the perspective of the Address
:
@address = Address.new(contact: @contact)
That will assign the new @address
your current @contact
to start with, but it will NOT modify the @contact
's list of addresses, which is what you want here.