Forum: Ruby on Rails get loop values into controller from view

Posted by amruby (Guest)
on 2012-06-19 12:07
(Received via mailing list)
<%= form_tag({:action => 'getrate', :controller => 'users'},{:method =>
:post}) do %>
        <br><br>
          *<% @eachskill.each do |loc| %>*
            <%= submit_tag  loc ,:type => "button", :id => loc 
,:disabled
=>"disabled", :class => "btn span2" %>
            &nbsp;
            <%= select_tag( loc,
"<option></option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option>".html_safe,:class
=> "span1")%>
            <br>
          *<% end %>*
          <%= submit_tag "Rate", :class => "btn" %>
 <% end %>

Here i need to get all the values from select_tag,  into the controller,
Posted by Robert Walker (robert4723)
on 2012-06-19 17:04
amruby wrote in post #1065102:
> <%= form_tag({:action => 'getrate', :controller => 'users'},{:method =>
> :post}) do %>
>         <br><br>
>           *<% @eachskill.each do |loc| %>*
>             <%= submit_tag  loc ,:type => "button", :id => loc
> ,:disabled
> =>"disabled", :class => "btn span2" %>
>             &nbsp;
>             <%= select_tag( loc,
> 
"<option></option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option>".html_safe,:class
> => "span1")%>
>             <br>
>           *<% end %>*
>           <%= submit_tag "Rate", :class => "btn" %>
>  <% end %>
>
> Here i need to get all the values from select_tag,  into the controller,

You are thinking backwards. Data should not flow from view to 
controller. It's the controller's job to provide that data to the view. 
Not the other way around.

>             <%= select_tag( loc,
> 
"<option></option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option>".html_safe,:class
> => "span1")%>

This list of options is data. Move this data out of the view. If this is 
simply a static list of options then you might simply put this into an 
instance variable in the controller. Then use the helpers provided by 
Rails to generate the HTML for the options list. Having to put that 
.html_safe directly into the view should be a trigger that you're doing 
something wrong.

See collection_select, options_from_collection_for_select or the other 
related helpers. There are enough of these helpers to cover just about 
any situation. I'm sure one of them will suit your needs.

In any case pushing data from view to controller is not the right 
solution.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.