miklki14567
miklki14567

Reputation: 149

Editing a comment that belongs to a post. Receiving a "No route matches" error

I have created a lightweight CRM where users can save contacts and leave notes on them. I want users to be able to edit their comments.

Alright, here what I have completed so far:

My Controller:

    def edit
      @contact = Contact.find(params[:contact_id])
      @note = @contact.notes.find(params[:id])
    end

   def update
    @contact = Contact.find(params[:contact_id])
    @note = @contact.notes.find(params[:id])

    if @note.update(notes_params)
        redirect_to contact_path(@contact)
    else
        render 'edit'
    end
  end

My Routes:

resources :contacts do 
  resources :notes
end

My Rake Routes:

new_contact_note GET /contacts/:contact_id/notes/new(.:format)  notes#new

edit_contact_note GET  /contacts/:contact_id/notes/:id/edit(.:format) notes#edit

 contact_note GET /contacts/:contact_id/notes/:id(.:format) notes#show

View File Link: (I think this is the culprit of the issue)

<p>
 <%= link_to 'Edit Note', edit_contact_note_path(@contact, @note) %>
</p>

Then when I try to edit a note I get this error:

No route matches {:action=>"edit", :contact_id=>"1", :controller=>"notes", :id=>nil}, missing required keys: [:id]

(If you need any other info please just let me know and I will give them)

Thank you very much ahead of time!

Here is the code

First from the _form.html.erb file which renders the form note.

<%= form_with(model: [ @contact, @contact.notes.build],
local: true) do |form| %>

<p>
  <%= form.label :title %><br>
  <%= form.text_field :title %>
</p>

<p>
  <%= form.label :body %><br>
  <%= form.text_area :body %>
</p>

<p>
  <%= form.submit %>
</p>

<% end %>

This is the _note.html.erb view which renders the edit link for notes:

<p>
  <strong>Note Title:</strong>
  <%= note.title %>
</p>

<p>
  <strong>Note Body:</strong>
  <%= note.body %>
</p>

<p>
  <%= link_to 'Delete Note', [note.contact, note],
            method: :delete,
            data: { confirm: 'Are you sure about the Note going away?' } %>

<p>
 <%= link_to 'Edit Note', edit_contact_note_path(@contact, @note) %>
</p>

Then here is all of the controller code for notes_controller.rb

class NotesController < ApplicationController
def create
    @contact = Contact.find(params[:contact_id])
    @note = @contact.notes.create(note_params)
    redirect_to contact_path(@contact)
end

def destroy
    @contact = Contact.find(params[:contact_id])
    @note = @contact.notes.find(params[:id])
    @note.destroy
    redirect_to contact_path(@contact)
end

def edit
    @contact = Contact.find(params[:contact_id])
    @note = @contact.notes.find(params[:id])
end

def update
    @contact = Contact.find(params[:contact_id])
    @note = @contact.notes.find(params[:id])

    if @note.update(notes_params)
        redirect_to contact_path(@contact)
    else
        render 'edit'
    end
end


private
def note_params
    params.require(:note).permit(:title, :body)
end
end

Here is the code from the show.html.erb for the contacts section. It's where my error says is coming from:

<p>
  <strong>First Name:</strong>
  <%= @contact.first_name %>
</p>
<p>
   <strong>Last Name:</strong>
   <%= @contact.last_name %>
   </p>
   <hr>
   <h2>Notes:</h2>

   <%= render @contact.notes %>
   <hr>
   <h2>Add a Note:</h2>
   <%= render 'notes/form' %>
   <br>
   <hr>
   <%= link_to 'Back', contacts_path %>
   <%= link_to 'Edit', edit_contact_path(@contact) %>

Upvotes: 0

Views: 64

Answers (1)

Manuel Martinez
Manuel Martinez

Reputation: 388

From the error message it seems like @note is nil, the "missing required keys: [:id]" part of the error message gives it away.

Check that @note isn't nil in the view where the edit link is shown and that it has an id associated :)

Upvotes: 3

Related Questions