views:

23

answers:

2

Hi Everyone,

In my rails application I have two models called Kases and Notes. They work in the same way comments do with blog posts, I.e. each Kase entry can have multiple notes attached to it.

I have got everything working, but for some reason I cannot get the destroy link to work for the Notes. I think I am overlooking something that is different with associated models to standard models.

Notes Controller

class NotesController < ApplicationController  
  # POST /notes
  # POST /notes.xml
  def create
    @kase = Kase.find(params[:kase_id])
    @note = @kase.notes.create!(params[:note])
    respond_to do |format|
      format.html { redirect_to @kase }
      format.js
    end
  end

end

Kase Model

class Kase < ActiveRecord::Base
  validates_presence_of :jobno
  has_many :notes

Note Model

class Note < ActiveRecord::Base
  belongs_to :kase
end

In the Kase show view I call a partial within /notes called _notes.html.erb:

Kase Show View

<div id="notes">    

        <h2>Notes</h2>
            <%= render :partial => @kase.notes %>
            <% form_for [@kase, Note.new] do |f| %>
                <p>
                    <h3>Add a new note</h3>
                    <%= f.text_field :body %><%= f.submit "Add Note" %>
                </p>
            <% end %>
    </div>

/notes/_note.html.erb

<% div_for note do %>
<div id="sub-notes">
  <p>
  <%= h(note.body) %><br />
  <span style="font-size:smaller">Created <%= time_ago_in_words(note.created_at) %> ago on <%= note.created_at %></span>
  </p>

<%= link_to "Remove Note", kase_path(@kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

</div>
<% end %>

As you can see, I have a Remove Note destroy link, but that destroys the entire Kase the note is associated with. How do I make the destroy link remove only the note?

<%= link_to "Remove Note", kase_path(@kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

Any help would, as always, be greatly appreciated!

Thanks,

Danny

+1  A: 
<%= link_to "Remove Note", note_path(note), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

you also will need the following entry in config/routes.rb (check if it already exists)

map.resources :notes

and check for following method in your NotesController

def destroy
  @note = Note.find(params[:id])
  @note.destroy
  .... # some other code here
end 

there's also another way of doing that if you don't have a NotesController and don't want to have it

zed_0xff
Great, it was the map.resources that I was missing. Woops!Thanks!
dannymcc
+1  A: 

You're calling the delete method on a kase -t hat's why it's deleting a kase. There's nothing in this link

<%= link_to "Remove Note", kase_path(@kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

apart from the text that even mentions a note - so why would it delete a note? Try

<%= link_to "Remove Note", note_path(note), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

This assumes you have the standard restful routes and actions set up.

As an additional point, you should never use link_to for non-get actions, because

  1. google spiders and the like will click on them. You might say 'they can't because you need to be logged in' and that's true but it's still not a good idea.
  2. if someone tries to open the link in a new tab/window it will break your site, or go to the wrong page, since it will try to open that url but with a get instead of a delete.
  3. generally, in web design, links should take you somewhere and buttons should 'do stuff', ie make changes. A destructive action like this therefore belongs on a button not a link.

Use button_to instead, which constructs a form to do that same thing.
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002420&amp;name=button_to

Max Williams
link_to with :method => :delete OR :confirm set is also creating a <form> tag, so it's safe to use
zed_0xff
I wasn't aware of the button_to option to be honest!Thanks for the answer, I know I didnt reference note in the link posted here - I have tried a number of different variations and that was the one that did *something* even if it wasnt the desired action.Thanks again!
dannymcc