# Passing params FROM Views TO Controllers -- using form\_for, form\_tag/select\_tag

**URL:** <https://www.ruby-forum.com/t/passing-params-from-views-to-controllers-using-form-for-form-tag-select-tag/138048>\
**Category:** Rails\
**Created:** [May 19, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/passing-params-from-views-to-controllers-using-form-for-form-tag-select-tag/138048 "2008-05-19T20:16:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgtorre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rgtorre](https://www.ruby-forum.com/u/rgtorre)\
**Post date:** [May 19, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/passing-params-from-views-to-controllers-using-form-for-form-tag-select-tag/138048/1 "2008-05-19T20:16:32Z")

</div>

Hello,

I have 3 methods in a controller (index, choose\_table and profile).

The index method simply displays a form\_for template where the user  
can choose a database name  
to use and pass the information to the action :choose\_table the  
params[:db][:type].

The choose\_table action then displays another form\_for template where  
the user chooses a table  
name that will be passed to the the action :profile as params[:table]  
[:type].

At the profile method, I then eval the expression using @db\_table =  
eval("#{@db\_env}" + “::” + “#{@table\_type}”)  
so I can do something like @db\_table.find(:all) which evaluates to  
Databasename::User.find(:all) or  
a @db\_table.paginate(:page etc, etc) which evaluates to something like  
Databasename:User.paginate(:page etc, etc).

The point is to give the user the ability to choose the database name  
and table using Dr. Nic’s  
Multiple Connections using the call format to  
Object::ModelName.methodname(…).

My question is how do I pass FROM the views TO the controller and  
retain information  
like the params[:db][:type] and params[:table][:type] which I lose  
when coming from a view or views.

What technique can I use to pass back to the controller params[:db]  
[:type] and params[:table][:type]  
from the views choose\_table and profile in this cases, using form\_for  
and form\_tag/select\_tag form methods. Passing these

params the other way around (from the  
controller to the view) is not a problem. BTW, I am unable to use  
session database persistence  
because I am dealing with non-standard legacy Oracle databases/tables.

Thanks  
rgtorre

* * *

## ##index method in controller def index end

## ##choose\_table method in controller def choose\_table #flash[:notice] = params[:db][:type] # just tried flash notice…kludgy…awkward way of #passing params … end

## ##profile method in controller def profile @db\_table = eval("#{@db\_env}" + “::” + “#{@table\_type}”) if params[:client\_id].nil? or params[:client\_id].empty? @users = @db\_table.paginate(:page =\> params[:page], :order =\> :client\_id, :per\_page =\> 10) else @users = @db\_table.paginate\_by\_client\_id params[:client\_id], :page =\> params[:page] params[:client\_id] = nil end end

 \<%= error\_messages\_for 'db' %\> Please Choose a Database Environment: \<% form\_for :db, :url =\> { :action =\> :choose\_table } do |form| %\> 

Database: \<%= form.select :type, SourceDb::SOURCE\_DB, :prompt =\> "Select a Database" %\>

 \<%= submit\_tag "Continue", :class =\> "submit" %\> \<% end %\>

* * *

\<%= error\_messages\_for ‘table’ %\>  
Please Choose a SWOT Table:  
\<% form\_for :table, :url =\> { :action =\> :profile } do |form| %\>

\<%=  
form.select :type,  
SelectTable::SELECT\_TABLES,  
:prompt =\> “Select a Table”  
%\>

  
\<%= submit\_tag “Continue”, :class =\> “submit” %\>  
\<% end %\>

* * *

 \<% form\_tag :action =\> :profile do %\> Client ID \<%= select\_tag "client\_id", options\_for\_select([["Select All", nil]] + #@db\_table.find(:all, :order =\> "client\_id").map {|a| [a.client\_id, a.client\_id] }) %\> \<%= submit\_tag "Filter" %\> \<% end %\>

# USER \<%="#{@db\_env}" %\>List

```
<% for user in @users %>
  <tr valign="top" class="<%= cycle('list-line-odd', 'list-line-

```

even’) %\>"\>

```
    <td> <%= user.client_id %> </td>
    <td> <%= user.field_2 %> </td>
    <td> <%= user.field_3 %> </td>
    <td> <%= user.field_4 %> </td>
    <td> <%= user.field_5 %> </td>
    <td> <%= user.field_6 %> </td>
    <td> <%= user.field_7.strftime('%m/%d/%Y') %> </td>
    <td> <%= user.field_8 %> </td>
    <td> <%= user.field_9.to_i %> </td>
    <td> <%= user.field_10 %> </td>

  </tr>
<% end %>

```

| Client Id | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | Field 7 | Field 8 | Field 9 | Field 10 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |

 \<%= page\_entries\_info @users %\> 
 \<%= will\_paginate @users, :container =\> false %\>
-------------------------------------------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![rgtorre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/839c29/32.png) [@rgtorre](https://www.ruby-forum.com/u/rgtorre)\
**Post date:** [May 19, 2008, 8:38pm UTC](https://www.ruby-forum.com/t/passing-params-from-views-to-controllers-using-form-for-form-tag-select-tag/138048/2 "2008-05-19T20:38:56Z")

</div>

On 19 May 2008, at 19:12, rgtorre wrote:

> Hello,
> 
> At the profile method, I then eval the expression using @db\_table =  
> eval("#{@db\_env}" + “::” + “#{@table\_type}”)

I would use constantize. What you’ve got there leaves you wide open.

> My question is how do I pass FROM the views TO the controller and  
> retain information  
> like the params[:db][:type] and params[:table][:type] which I lose  
> when coming from a view or views.
> 
> What technique can I use to pass back to the controller params[:db]  
> [:type] and params[:table][:type]  
> from the views choose\_table and profile in this cases, using form\_for  
> and form\_tag/select\_tag form methods. Passing these

hidden\_field\_tag? or make your routes interesting, ie /profile/foo/bar  
runs the profile action for database foo and table bar.

> params the other way around (from the  
> controller to the view) is not a problem. BTW, I am unable to use  
> session database persistence  
> because I am dealing with non-standard legacy Oracle databases/tables.

You could still use the session if you wanted (eg with cookiestore).  
And the fact that you tried to use the flash indicates your session is  
working fine (the flash is stored in the session).

Fred
