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]

<%= link_to "option A", :controller => "scriptrunner", :action => "runoptionA" %> <%= link_to "option B", :controller => "scriptrunner", :action => "runoptionB" %> <%= link_to "option C", :controller => "scriptrunner", :action => "runoptionC" %> [/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?

On Fri, Jul 20, 2012 at 1:22 PM, lalalalala pqpqpqpqpq
[email protected] wrote:

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.


Hassan S. ------------------------ [email protected]

twitter: @hassan

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]

<%= link_to "option A", :controller => "scriptrunner", :action => "runoptionA" %> <%= link_to "option B", :controller => "scriptrunner", :action => "runoptionB" %> <%= link_to "option C", :controller => "scriptrunner", :action => "runoptionC" %> [/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 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.

On Mon, Jul 23, 2012 at 7:59 AM, lalalalala pqpqpqpqpq
[email protected] wrote:

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

twitter: @hassan

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.

Hassan S. wrote in post #1069823:

Do you know HTML? Do you know JavaScript?


Hassan S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

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

Hassan S. wrote in post #1069829:

On Mon, Jul 23, 2012 at 8:39 AM, lalalalala pqpqpqpqpq
[email protected] wrote:

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 S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

I have to make this app for my internship, could you just guide me how
to make the submit button? I’m guessing I’ll have to make a form which
passes the selected option to an action in my controller?

On Mon, Jul 23, 2012 at 8:39 AM, lalalalala pqpqpqpqpq
[email protected] wrote:

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

twitter: @hassan

Hassan S. wrote in post #1069841:

On Mon, Jul 23, 2012 at 8:53 AM, lalalalala pqpqpqpqpq
[email protected] wrote:

I have to make this app for my internship, could you just guide me how
to make the submit button? I’m guessing I’ll have to make a form which
passes the selected option to an action in my controller?

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


Hassan S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

How would I pass the id of an option from the view to the controller? As
in, what command in the view? And how would I access this id that is
passed, in the controller?

On Mon, Jul 23, 2012 at 8:53 AM, lalalalala pqpqpqpqpq
[email protected] wrote:

I have to make this app for my internship, could you just guide me how
to make the submit button? I’m guessing I’ll have to make a form which
passes the selected option to an action in my controller?

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


Hassan S. ------------------------ [email protected]

twitter: @hassan

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?

On Mon, Jul 23, 2012 at 11:28 AM, lalalalala pqpqpqpqpq
[email protected] wrote:

How would I pass the id of an option from the view to the controller? As
in, what command in the view? And how would I access this id that is
passed, in the controller?

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,

Hassan S. ------------------------ [email protected]

twitter: @hassan

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.

On Mon, Jul 23, 2012 at 12:12 PM, lalalalala pqpqpqpqpq
[email protected] wrote:

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


Hassan S. ------------------------ [email protected]

twitter: @hassan

This is the view source after I press go:

optiona optionb optionc

On Mon, Jul 23, 2012 at 12:42 PM, lalalalala pqpqpqpqpq
[email protected] wrote:

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.


Hassan S. ------------------------ [email protected]

twitter: @hassan

what do you mean by log entry.

On Mon, Jul 23, 2012 at 1:07 PM, lalalalala pqpqpqpqpq
[email protected] wrote:

This is the view source after I press go:

Yeah, that’s 1 of 2… :slight_smile:


Hassan S. ------------------------ [email protected]

twitter: @hassan

On 20 July 2012 21:22, lalalalala pqpqpqpqpq [email protected]
wrote:

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

On Mon, Jul 23, 2012 at 1:19 PM, lalalalala pqpqpqpqpq
[email protected] wrote:

what do you mean by log entry.

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,

Hassan S. ------------------------ [email protected]

twitter: @hassan