How to add button to pass drop down menu selection to controller

Hi, I want to link the selections from a drop down menu in my view to a
controller action. Here's the code for my view:

[code]<select>
<option><%= link_to "option A", :controller => "scriptrunner", :action
=> "runoptionA" %></option>
<option><%= link_to "option B", :controller => "scriptrunner", :action
=> "runoptionB" %></option>
<option><%= link_to "option C", :controller => "scriptrunner", :action
=> "runoptionC" %></option>
</select>
[/code]

Right now when I select an option from the drop down menu, nothing is
called in my controller. Is there a way to add a "GO" button, so after I
select something from the menu, I can click the GO button and it will
call the corresponding action in my controller?

Right now when I select an option from the drop down menu, nothing is
called in my controller.

Why would you expect it to be?

Is there a way to add a "GO" button, so after I
select something from the menu, I can click the GO button and it will
call the corresponding action in my controller?

Of course. It's just markup. Add whatever elements you want. Or add
the JavaScript to trigger your form submission on the select `change`
event. Either way is trivial.

lalalalala pqpqpqpqpq wrote in post #1069545:

Hi, I want to link the selections from a drop down menu in my view to a
controller action. Here's the code for my view:

[code]<select>
<option><%= link_to "option A", :controller => "scriptrunner", :action
=> "runoptionA" %></option>
<option><%= link_to "option B", :controller => "scriptrunner", :action
=> "runoptionB" %></option>
<option><%= link_to "option C", :controller => "scriptrunner", :action
=> "runoptionC" %></option>
</select>
[/code]

Are you saying you literally have written the HTML for the select
control in your view template? If so then you should know that Rails
provides some nice helpers for that.

Right now when I select an option from the drop down menu, nothing is
called in my controller.

Of course the controller doesn't get called. HTML doesn't work that way.
A request is sent to the server, the server responds with HTML (or other
response types). At this point the server waits for other requests.

The client will generate another request on certain actions. 1. A
hyperlink is clicked or 2. A form is submitted. I think those are the
only two actions that will trigger a new request, without the aid of
something outside of the HTML. That might be JavaScript, Flash or other
browser plugins.

Changing the selection in a popup box, clicking a checkbox, etc. will
not send a request to the server. These actions only modify the form
data that gets POSTed back to the server when a form gets submitted.

Is there a way to add a "GO" button, so after I
select something from the menu, I can click the GO button and it will
call the corresponding action in my controller?

Sure. That is called a form submission. That's what the <input
type="submit"> is.

However, in most cases a better way would be to use JavaScript/JQuery to
bind a JavaScript function to the "change" event of the select tag. Then
post the data you need back to the server asynchronously using
XMLHttpRequest (A.K.A AJAX).

But, only use AJAX is it's really necessary. In many cases you can pass
everything you need in the initial request and then use JavaScript
(without AJAX) to control your user interface.

One common use that typically leads to questions like yours, is when one
wants to control one popup's selection based on a another popup.
Sometimes it's possible to send all the information the client needs to
accomplish that in the initial request. Thus saving additional round
trips to the server. Other times that's not possible, or not convenient,
and it's necessary to call back to the server.

The one thing I would highly recommend is to avoid triggering a page
refresh based on changes to popups or checkboxes. Use JavaScript (and
AJAX if necessary) to do partial updates to your page. Your users will
thank you for that. It's highly annoying to have your browser window
scrolled to exactly where you want it then changing a popup causes a
page refresh and throws you back to top of the page. This would happen
if you were do use a normal form submit ("Go") button as you mentioned.

Ok, i changed my view to
<%= select_tag "options", options_for_select(@options, "") %>

and in my controller I have:
def index
@options = ["A", "B", "C"]
end

Now, how would I add a submit button in my view so when some1 has for
example, "A" selected, the action runA would be called in my controller.

Using the same suggestions you already got for the same question?

It's either a form and a submit button -- standard HTML -- if you want
(or can at least live with) a page refresh, or AJAX/JavaScript to run
your method while staying on the same page.

Do you know HTML? Do you know JavaScript?

Hassan Schroeder wrote in post #1069823:

Do you know HTML? Do you know JavaScript?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Sorry, I'm new to HTML.. I'm struggling implementing the submit button.

Then trying to develop a web app might be a little premature... :slight_smile:

You should find a tutorial on how web forms work and understand
the basics first. IMO.

Hassan Schroeder wrote in post #1069829:

Yes. Unless you use JavaScript, which I'll assume isn't an option at
this point :slight_smile:

Hassan Schroeder wrote in post #1069841:

You put your select in a form and submit it. That's what forms do --
send name/value pairs to the web server.

Hence my suggestion that you make some effort to understand the
basics before trying to create a web app :slight_smile:

If nothing else, you can look at a scaffolded form as an example to
see how it looks as markup (`view source`) and compare to what's
being sent to the server, which will be in your Rails log.

HTH,

This is what my index.html.erb looks like:

<% form_tag(:controller => 'opt', :action => 'index', :id => 1) do %>
   <%= select_tag "option", options_for_select(@options, "") %>
   <%= submit_tag "go"%>
<%end%>

In my controller, for my index method I have:

class optController < ApplicationController

def index
@options = ["Option A", "Option B", "Option C"]
end

As you can see I'm only passing the id: 1 everytime I click go. How
would I make it so the selected choice's id is passed instead. For
example if I choose Option B from the menu, 2 should be passed. Then I
can access this 2 in my controller through some variable maybe?

<% form_tag(:controller => 'opt', :action => 'index', :id => 1) do %>
   <%= select_tag "option", options_for_select(@options, "") %>
   <%= submit_tag "go"%>
<%end%>

In my controller, for my index method I have:

class optController < ApplicationController

which is wrong - s/b OptController

def index
@options = ["Option A", "Option B", "Option C"]
end

As you can see I'm only passing the id: 1 everytime I click go.

No, you're not. Again: look at your page using 'view source' in your
browser. Look *closely* at your form. Then go back and re-read the
doc for options_for_select.

Now select something, submit the form and see what's logged.

Ok, I changed :id => '1' to :id => 'params[:name][:id]'

then, I checked the view source, but no matter what option is selected,
when I press go, the option value is always set to option A... Even when
I view source before pressing go, option valye is set to option A.

Ok, I changed :id => '1' to :id => 'params[:name][:id]'

In the form_tag?? Both irrelevant and wrong.

then, I checked the view source, but no matter what option is selected,
when I press go, the option value is always set to option A... Even when
I view source before pressing go, option valye is set to option A.

Paste the `view source` section showing your form, and the log entry
from submitting it.

This is the view source after I press go:

<form action="/opt/index/1" method="post"><div
style="margin:0;padding:0;display:inline"><input
name="authenticity_token" type="hidden"
value="yjZGVeHRmJ/tolJ9C1TzkoVguf5olWH8+CMO6Egvg0w=" /></div>
   <select id="option" name="option"><option
value="optiona">optiona</option>
<option value="optionb">optionb</option>
<option value="optionc">optionc</option> </select>

   <input name="commit" type="submit" value="go" />
</form>

Yeah, that's 1 of 2... :slight_smile:

what do you mean by log entry.

Hi, I want to link the selections from a drop down menu in my view to a
controller action. Here's the code for my view:

[code]<select>
<option><%= link_to "option A", :controller => "scriptrunner", :action
=> "runoptionA" %></option>
<option><%= link_to "option B", :controller => "scriptrunner", :action
=> "runoptionB" %></option>
<option><%= link_to "option C", :controller => "scriptrunner", :action
=> "runoptionC" %></option>
</select>
[/code]

Right now when I select an option from the drop down menu, nothing is
called in my controller. Is there a way to add a "GO" button, so after I
select something from the menu, I can click the GO button and it will
call the corresponding action in my controller?

To learn the basics of Rails have a look at railstutorial.org, which
is free to use online. You will also learn about submitting forms and
so on.

Colin

I have no idea what your background is, but you really, really do need
to address the basics of web development, aside from anything Ruby-
or Rails-specific.

Good luck,