on my homepage i''d like a drop down. The ser selects the resort and it should go to the resorts show action the following code doesnt work ! how do i do it. rake routes shows me resort GET /resorts/:id(.:format) {:controller=>"resorts", :action=>"show"} simples? <% if @resorts %> <% form_tag resort_path(resort.id), :method => :get do %> <%= select_tag(:resort, "<option>-Select a resort..</option>" + options_from_collection_for_select(@resorts, :id, :name)) %> <%= submit_tag "Go" %> <% end -%> -- Posted via http://www.ruby-forum.com/.
what is the "resort.id" in "resort_path(resort.id)? it has no value.. also, im not sure its a good idea to submit a form for a simple get request like this. forms and get requests, as far as i can imagine, go well together for something like a search. try something like this.. <%= select_tag(:resort, "<option>-Select a resort</ option>"+options_from_collection_for_select (@resorts, :id, :name), :onchange => "new Ajax.Request(''/resorts/'', {asynchronous:true, evalScripts:true, method:''get'', parameters:''id='' + encodeURIComponent(value)})"%> im not sure if the ajax request uri is correct for the show action, u may have to look it up. what this does is, on change of the selection, it shoots off an ajax request to the resorts controller show action with the id of the resort selected. the rest will be done by the controller show action. good luck! On Aug 1, 6:01 pm, bingo bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> on my homepage i''d like a drop down. > > The ser selects the resort and it should go to the resorts show action > > the following code doesnt work ! > > how do i do it. > > rake routes shows me > resort GET /resorts/:id(.:format) {:controller=>"resorts", > :action=>"show"} > > simples? > > <% if @resorts %> > <% form_tag resort_path(resort.id), :method => :get do %> > <%= select_tag(:resort, "<option>-Select a resort..</option>" + > options_from_collection_for_select(@resorts, :id, :name)) %> > <%= submit_tag "Go" %> > <% end -%> > -- > Posted viahttp://www.ruby-forum.com/.
Hi Ram, That looks ideal. Your thinking is absolutely on the right lines in that I''m simply looking for the select to allow the user to jump to the show action of the resorts controller passing in the resort ID. Using Ajax to do this is fine by me. Just a few questions 1) I can''t get the code to work as it is, I''m getting a syntax error - figuring that there''s a bracket missing or something - I did try to correct but couldn''t seem to! 2) For the Ajax request to work do I need to do anything else special in the app to allow it to work? Cheers, bb -- Posted via http://www.ruby-forum.com/.
This should work <%= select_tag :resort_id, "<option>-Select a resort</ option>"+options_from_collection_for_select (@resorts, :id, :name), :onchange => "new Ajax.Request(''/resorts/''+ (value), {asynchronous:true, evalScripts:true, method:''get'', parameters:''id='' + encodeURIComponent(value)});" %> Check the dev log for debugging if you run into any problems. You can also use FF''s Firebug to debug JS. On Aug 3, 1:07 pm, bingo bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi Ram, > > That looks ideal. Your thinking is absolutely on the right lines in that > I''m simply looking for the select to allow the user to jump to the show > action of the resorts controller passing in the resort ID. Using Ajax to > do this is fine by me. > > Just a few questions > > 1) I can''t get the code to work as it is, I''m getting a syntax error - > figuring that there''s a bracket missing or something - I did try to > correct but couldn''t seem to! > > 2) For the Ajax request to work do I need to do anything else special in > the app to allow it to work? > > Cheers, > > bb > -- > Posted viahttp://www.ruby-forum.com/.
You will need to also send the authenticity token along with Ajax requests if its a request besides a GET request. Module: ActionController::RequestForgeryProtection::ClassMethods<http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html> Read up on RJS, Prototype JS and Rails'' Javascript Helpers. Its a lot of fun! Good luck.. 2009/8/3 Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> > This should work > > <%= select_tag :resort_id, "<option>-Select a resort</ > option>"+options_from_collection_for_select > (@resorts, :id, :name), :onchange => "new Ajax.Request(''/resorts/''+ > (value), {asynchronous:true, evalScripts:true, method:''get'', > parameters:''id='' + encodeURIComponent(value)});" %> > > Check the dev log for debugging if you run into any problems. You can > also use FF''s Firebug to debug JS. > > On Aug 3, 1:07 pm, bingo bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > Hi Ram, > > > > That looks ideal. Your thinking is absolutely on the right lines in that > > I''m simply looking for the select to allow the user to jump to the show > > action of the resorts controller passing in the resort ID. Using Ajax to > > do this is fine by me. > > > > Just a few questions > > > > 1) I can''t get the code to work as it is, I''m getting a syntax error - > > figuring that there''s a bracket missing or something - I did try to > > correct but couldn''t seem to! > > > > 2) For the Ajax request to work do I need to do anything else special in > > the app to allow it to work? > > > > Cheers, > > > > bb > > -- > > Posted viahttp://www.ruby-forum.com/. > > >-- In Sport We Trust !!! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Gents - am sure that''ll be perfect for me, very interesting this Ajax stuff. I''ll try it later and report back! bb -- Posted via http://www.ruby-forum.com/.
SO CLOSE ! The select dropdown works fine and I can see from the dev log that it''s trying to jump to the show action like this... Completed in 40ms (View: 28, DB: 1) | 200 OK [http://localhost/resorts/15?id=15] But it won''t work, that''s because I guess I need thte resulting URL to be simply.. [http://localhost/resorts/15] How do I do that - skip off the ?id=15 ? cheers bb. -- Posted via http://www.ruby-forum.com/.
damn i thought i had fixed by simply removing the last bit of the line so it reads.. <%= select_tag :id, "<option>-Select a resort</option>" + options_from_collection_for_select (@resorts, :id, :name), :onchange => "new Ajax.Request(''/resorts/''+(value), {asynchronous:true, evalScripts:true, method:''get''});" %> that fixes the URL - but bizarely it still doesnt jump to the show page! here''s the log output - WIERD, it says it''s getting rendered but its not there in my browser! Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 11:48:37) [GET] Parameters: {"id"=>"15"} Resort Load (0.5ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 15) Rendering template within layouts/resorts Rendering resorts/show Advert Load (0.2ms) SELECT * FROM "adverts" INNER JOIN "adverts_resorts" ON "adverts".id = "adverts_resorts".advert_id WHERE ("adverts_resorts".resort_id = 15 ) Rendered resorts/_stats (0.4ms) Rendered shared/_flash (0.1ms) User Load (0.4ms) SELECT * FROM "users" WHERE ("users"."persistence_token" = ''67e079e68b29c2c2ab10afaf829e4fbe07d1f3b1a05bfeb0936c0cf6fbb9b799218a360adbc69a5dd92364cadeefaa7167cf55210bf1b0ad73dfb4516b6b8b30'') LIMIT 1 Rendered shared/_menu (85.3ms) Rendered shared/_footer (0.1ms) Completed in 109ms (View: 97, DB: 1) | 200 OK [http://localhost/resorts/15] -- Posted via http://www.ruby-forum.com/.
You still need to write controller code to respond to ajax requests under the respond_to block @resort = Resort.find(params[:id]) respond_to do |format| format.html #default show.html.erb format.js { redirect_to resort_path(@resort) } end 2009/8/3 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > damn i thought i had fixed by simply removing the last bit of the line > so it reads.. > > <%= select_tag :id, "<option>-Select a resort</option>" + > options_from_collection_for_select (@resorts, :id, :name), > :onchange => "new Ajax.Request(''/resorts/''+(value), > {asynchronous:true, evalScripts:true, method:''get''});" %> > > that fixes the URL - but bizarely it still doesnt jump to the show page! > > here''s the log output - WIERD, it says it''s getting rendered but its not > there in my browser! > > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 11:48:37) > [GET] > Parameters: {"id"=>"15"} > Resort Load (0.5ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 15) > Rendering template within layouts/resorts > Rendering resorts/show > Advert Load (0.2ms) SELECT * FROM "adverts" INNER JOIN > "adverts_resorts" ON "adverts".id = "adverts_resorts".advert_id WHERE > ("adverts_resorts".resort_id = 15 ) > Rendered resorts/_stats (0.4ms) > Rendered shared/_flash (0.1ms) > User Load (0.4ms) SELECT * FROM "users" WHERE > ("users"."persistence_token" > > ''67e079e68b29c2c2ab10afaf829e4fbe07d1f3b1a05bfeb0936c0cf6fbb9b799218a360adbc69a5dd92364cadeefaa7167cf55210bf1b0ad73dfb4516b6b8b30'') > LIMIT 1 > Rendered shared/_menu (85.3ms) > Rendered shared/_footer (0.1ms) > Completed in 109ms (View: 97, DB: 1) | 200 OK > [http://localhost/resorts/15] > > -- > Posted via http://www.ruby-forum.com/. > > > >-- In Sport We Trust !!! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ok, show in resorts controller now looks like this. def show @resort = Resort.find(params[:id]) respond_to do |format| format.html #default show.html.erb format.js { redirect_to resort_path(@resort) } end end but still no dice ! ? -- Posted via http://www.ruby-forum.com/.
hmmm.. error messages? log outputs? does the Resort find sql query shoot off? from my experiments, the only workable solution is using render :update.. format.js { render :update do |page| page.redirect_to resort_path(@resort) end } but this actually sounds silly cos you''re in the resorts controller show action and you are redirecting to yourself. On Aug 3, 4:49 pm, bingo bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> ok, show in resorts controller now looks like this. > > def show > @resort = Resort.find(params[:id]) > > respond_to do |format| > format.html #default show.html.erb > format.js { redirect_to resort_path(@resort) } > end > > end > > but still no dice ! ? > -- > Posted viahttp://www.ruby-forum.com/.
ahhhh,, seems like its doing it multiple times.. wierd..notice the time stamps. never does get me to the show action though. Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.7ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 108ms (DB: 1) | 302 Found [http://localhost/resorts/18?id=18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (1.4ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 15ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.5ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.5ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.7ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 13ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 82ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) [GET] Parameters: {"id"=>"18"} Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 13ms (DB: 1) | 302 Found [http://localhost/resorts/18] Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:07) [GET] Parameters: {"id"=>"18"} Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" = 18) Redirected to http://localhost:3000/resorts/18 Completed in 13ms (DB: 1) | 302 Found [http://localhost/resorts/18] -- Posted via http://www.ruby-forum.com/.
Do you still have format.html ? render :update works for me. and i cant think of a cleaner way to do this.. On Aug 3, 5:31 pm, bingo bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> ahhhh,, > > seems like its doing it multiple times.. wierd..notice the time stamps. > > never does get me to the show action though. > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.7ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 108ms (DB: 1) | 302 Found > [http://localhost/resorts/18?id=18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (1.4ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 15ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.5ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.5ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 12ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.7ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 13ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 82ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:06) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 13ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > Processing ResortsController#show (for 127.0.0.1 at 2009-08-03 13:29:07) > [GET] > Parameters: {"id"=>"18"} > Resort Load (0.6ms) SELECT * FROM "resorts" WHERE ("resorts"."id" > 18) > Redirected tohttp://localhost:3000/resorts/18 > Completed in 13ms (DB: 1) | 302 Found [http://localhost/resorts/18] > > -- > Posted viahttp://www.ruby-forum.com/.