?

Log in

Wed, Mar. 26th, 2008, 12:07 am
tirlasutaa: :action=>:destroy, symbols

I'm working through some book exercises & updating them to work with the Rails 2.0.2 that I've installed. Apart from the questions attached to the code samples below (I only left the javascript stuff that changed for readability), why is :method=>:delete (now vs before 2.0.* examples) required when :action=>:destroy is also present? Also, does anyone know of a reference that explains where or how symbols are defined? It's great to read that symbols are awesome, but that doesn't explain anything.

  1. Ok
    link_to( "Delete",
    { :id=>photo, :action=>:destroy },
    { :confirm => "Are you sure?", :method => :delete })
    <a href="/photos/1" onclick="javascript m.setAttribute('value', 'delete');">Delete</a>
  2. Why is :action=>:destroy necessary here, but not in #6?
    link_to( "Delete",
    { :id=>photo },
    { :confirm => "Are you sure?", :method=>:delete })
    <a href="/photos/index/1" onclick="javascript m.setAttribute('value', 'delete');">Delete</a>
  3. Ok
    link_to( "Delete",
    { :id=>photo.id, :action=>:destroy },
    { :confirm => "Are you sure?", :method => :delete })
    <a href="/photos/1" onclick="javascript m.setAttribute('value', 'delete');">Delete</a>
  4. Is :action=>:delete invalid because there is not a defined delete method in photos_controller.rb?
    link_to( "Delete",
    { :id=>photo.id, :action=>:delete },
    { :confirm => "Are you sure?", :method => :delete })
    <a href="/photos/delete/1" onclick="javascript m.setAttribute('value', 'delete');">Delete</a>
  5. Why is :action=>:destroy necessary here, but not in #6?
    link_to( "Delete",
    { :id=>photo.id },
    { :confirm => "Are you sure?", :method=>:delete })
    <a href="/photos/index/1" onclick="javascript m.setAttribute('value', 'delete');">Delete</a>
  6. Ok
    link_to( "Delete",
    photo,
    { :confirm => "Are you sure?", :method => :delete })
    <a href="/photos/1" onclick="javascript m.setAttribute('value', 'delete');">Delete</a>

Wed, Mar. 26th, 2008 05:13 am (UTC)
tirlasutaa

#2 & #5 were just experiments. I still don't understand why they need :action=>:destroy & #6 doesn't. (Or, why can #6 get away w/o :action=>:destroy? what about using :id=> makes them need :action=>:destroy?)


As for symbols, where does it say link_to can take :action? It's not listed as an option in url_for (that I could see) or anywhere else (although it's possible I skipped a couple functions somewhere when trying to figure it out). Where is it defined that :action can take things such as :destroy & where does :destroy come from? (Not trying to be dense or annoying, but these details are things I just can't accept & use without an understanding.)


Thank you for your help thus far. It's useful.

Wed, Mar. 26th, 2008 12:14 pm (UTC)
cpm

url_for takes a hash and :action is just one of the keys of the hash. url_for just takes the stuff in the hash and tries to make a pretty URL of it based on config/routes.rb.

Rails assumes "controller" and "action" are the controller and the action name when it parses the URL. However, when using their REST magic, you can use different HTTP request methods (GET, POST, DELETE, PUT) to let Rails infer the action.