?

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 04:39 am (UTC)
trythil

I'm a little confused by your examples (#2 and #5 don't include :action => :destroy, for example), but a lot of what you're seeing seems related to Rails 2.0 changes made to make RESTful resources easier to work with. The documentation on ActionController::Resources explains a good bit of what Rails 2 will do.

As far as symbols go: they're interned strings, and you can define them at any time, by using the :symbol syntax.

In Ruby, there's two large differences between symbols and strings:

(1) A symbol exists in only one place (interned).
(2) A symbol is immutable.

Here's an illustration of (1):

nevrast:~ trythil$ irb
irb(main):001:0> "abc".object_id
=> 36890
irb(main):002:0> "abc".object_id
=> 23470
irb(main):003:0> :abc.object_id
=> 158498
irb(main):004:0> :abc.object_id
=> 158498
irb(main):005:0> :"This is a test".object_id
=> 158638
irb(main):006:0> :"This is a test".object_id
=> 158638


Lines 1 and 2 create two separate strings that both contain the content abc. They're separate objects, however, and therefore occupy their own positions in Ruby's symbol table. The interned string referenced in lines 3 and 4 abc (represented as :abc), though, occupies only one place for as long as the program runs, regardless of how many times I reference it. Finally, lines 5 and 6 show an alternative syntax for symbols.

This can be useful as one tool for keeping garbage collection behavior sane: say, if you use the same short string in many places (as a keyword argument, for instance) and don't want to litter memory with string corpses.

Edited at 2008-03-26 04:44 am (UTC)

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.