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, “-Select a resort…” +
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, “-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!

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, “-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.

You will need to also send the authenticity token along with Ajax
requests
if its a request besides a GET request.
Module:
ActionController::RequestForgeryProtection::ClassMethodshttp://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 [email protected]

also use FF’s Firebug to debug JS.


Posted viahttp://www.ruby-forum.com/.


In Sport We Trust !!!

Thanks Gents - am sure that’ll be perfect for me, very interesting this
Ajax stuff. I’ll try it later and report back!

bb

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.

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 [email protected]

Rendering resorts/show
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 !!!

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 ! ?

damn i thought i had fixed by simply removing the last bit of the line
so it reads…

<%= select_tag :id, “-Select a resort” +
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]

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.

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]

Do you still have format.html ?

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

PHEW. Works ! :slight_smile: thanks.

with render :update that is.

ta

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs