Sorry for the newbie question, in working through the testing section of the Agile Rails book I came across an issue with the redirect_to command that I can''t seem to find the answer to in the book. When should you use the following syntax: redirect_to :action => :index and when should you use: redirect_to :action => ''index'' For example, on page 152 the sample code given uses :action => ''index ; however that code generated an error and the test will only work with action => :index. Then I noticed that in the controller code I had written (following the example in the book), sometimes the code was redirect_to :action => :method and at other times it was action => ''method'' At first I thought the syntax was interchangeable, but no, it isn''t. For example, in my login_controller_test.rb I have two different methods. One of them uses :action => :login and the other uses :action => ''index''. When I tried to change :login to ''login'' just to see if the syntax didn''t matter, Rails generated an undefined method error when running the test. So there is obviously a difference. But the book doesn''t say what the difference is or under what circumstances I should use :action => :method compared with :action => ''method''. -- "Impossible is nothing." _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 2-okt-2005, at 1:41, zer0halo wrote:> > > For example, in my login_controller_test.rb I have two different > methods. One of them uses :action => :login and the other > uses :action => ''index''. When I tried to change :login to ''login'' > just to see if the syntax didn''t matter, Rails generated an > undefined method error when running the test. So there is > obviously a difference. But the book doesn''t say what the > difference is or under what circumstances I should use :action > => :method compared with :action => ''method''.As I remember when you use a symbol it works like a _method reference on the controller_ when generating the url. For example: controller MyCtr < ApplicationController def process_cart # this is your action # do stuff redirect_to :action=>:action_depending_on_customer end private def action_depending_on_customer @customer.privileged? ''big_thank_you'' : ''small_thank_you'' end end On the contrary a string literal should work like the action name. Report if my guess is right ain''t got time to check now -- Julian "Julik" Tarkhanov
On 10/2/05, Julian ''Julik'' Tarkhanov <listbox-RY+snkucC20@public.gmane.org> wrote:> > On 2-okt-2005, at 1:41, zer0halo wrote: > > > > > > For example, in my login_controller_test.rb I have two different > > methods. One of them uses :action => :login and the other > > uses :action => ''index''. When I tried to change :login to ''login'' > > just to see if the syntax didn''t matter, Rails generated an > > undefined method error when running the test. So there is > > obviously a difference. But the book doesn''t say what the > > difference is or under what circumstances I should use :action > > => :method compared with :action => ''method''. > > As I remember when you use a symbol it works like a _method reference > on the controller_ when generating the url. For example: > > controller MyCtr < ApplicationController > def process_cart # this is your action > # do stuff > redirect_to :action=>:action_depending_on_customerI think that should be redirect_to :action => action_depending_on_customer. It''s not a symbol, it''s a function.> end > > private > def action_depending_on_customer > @customer.privileged? ''big_thank_you'' : ''small_thank_you'' > end > end > > On the contrary a string literal should work like the action name. > > Report if my guess is right ain''t got time to check now > > -- > Julian "Julik" Tarkhanov > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >