select helper

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 -%>

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</

"+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!

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

This should work

<%= select_tag :resort_id, "<option>-Select a resort</

"+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.

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

Read up on RJS, Prototype JS and Rails’ Javascript Helpers. Its a lot of fun!

Good luck…

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.

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\]

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

hmmm.. error messages? log outputs? does the Resort find sql query shoot off?

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.

Do you still have format.html ?

render :update works for me. and i cant think of a cleaner way to do this..