Unable to retrieve selected value

Hello,

This is my first day on rails. I’ve been trying to make a drop down menu
work all day but no joy.

--------Controller---------
class ScenariomgrController < ApplicationController
def runscenarios
@booknames = Obsrvbl.find(:all, :order => “name”).collect{ |o|
[o.name, o.id] }
end

def createipfile
render_text @booknames.id
end
end
----------View-----------

Observable Name


<%= select “booknames”, “id”, @booknames %>
<%= link_to “Run Scenario”, :action => ‘createipfile’%>

No matter what I do, clicking the link does not show me the selected
value. I have read 20+ posts for the select statement and read the api
but cannot see why I cannot see the selected value on clicking the “run
scenario” link to activate the createipfile action.

Is it that session variables are not shared between different
subroutines?

Thank you so much!
Aman

Aman Thind wrote:

No matter what I do, clicking the link does not show me the selected
value. I have read 20+ posts for the select statement and read the api
but cannot see why I cannot see the selected value on clicking the “run
scenario” link to activate the createipfile action.

Basically, it’s because a link doesn’t submit a form. It gets a new URL
from the page, in your case always /scenariomgr/createipfile .

You need to create a form that posts to createipfile. The form should
contain your select list element and a submit button. When that submit
button is clicked, the selected value will be passed to the createipfile
action, in which you can retrieve it from the params hash by doing
params[:booknames][:id].

If you insist on having a link submit your form you need to fiddle with
Javascript, but I’d recommend you start with the easy case, which is the
above.

PS: You might want to follow one or more of the tutorials from
http://www.rubyonrails.org/docs to learn more about the basic stuff like
passing parameters and what not.


Jakob S. - http://mentalized.net

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