rgtorre
2008-May-19 18:12 UTC
Passing params FROM Views TO Controllers -- using form_for, form_tag/select_tag
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 ------------------------------------------------------------------------------------------------- <!-- ! index.html.erb --> <div class="selectdb-form"> <%= error_messages_for ''db'' %> <legend>Please Choose a Database Environment:</legend> <% form_for :db, :url => { :action => :choose_table } do |form| %> <p> <label for="type">Database:</label> <% form.select :type, SourceDb::SOURCE_DB, :prompt => "Select a Database" %> </p> <%= submit_tag "Continue", :class => "submit" %> <% end %> </div> ------------------------------------------------------------------------------------------------- <!-- ! choose_table.html.erb --> <div class="choosetable-form"> <%= error_messages_for ''table'' %> <legend>Please Choose a SWOT Table:</legend> <% form_for :table, :url => { :action => :profile } do |form| %> <p> <label for="type"></label> <% form.select :type, SelectTable::SELECT_TABLES, :prompt => "Select a Table" %> </p> <%= submit_tag "Continue", :class => "submit" %> <% end %> </div> ------------------------------------------------------------------------------------------------- <!-- ! profile.html.erb --> <div class="box"> <% form_tag :action => :profile do %> <label for="client_id">Client ID</label> <%= 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 %> </div> <div id="list"> <h1>USER <%="#{@db_env}" %>List</h1> <table cellpadding="5" cellspacing="0"> <tr class="list-name"> <th>Client Id</th> <th>Field 2</th> <th>Field 3</th> <th>Field 4</th> <th>Field 5</th> <th>Field 6</th> <th>Field 7</th> <th>Field 8</th> <th>Field 9</th> <th>Field 10</th> </tr> <% 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 %> </table> </div> <div class="digg_pagination"> <div clas="page_info"> <%= page_entries_info @users %> </div> <%= will_paginate @users, :container => false %> </div> ------------------------------------------------------------------------------------------------- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-May-19 18:36 UTC
Re: Passing params FROM Views TO Controllers -- using form_for, form_tag/select_tag
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.> > 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 >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> 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 > ------------------------------------------------------------------------------------------------- > > <!-- > ! index.html.erb > --> > <div class="selectdb-form"> > <%= error_messages_for ''db'' %> > <legend>Please Choose a Database Environment:</legend> > <% form_for :db, :url => { :action => :choose_table } do |form| %> > <p> > <label for="type">Database:</label> > <%> form.select :type, > SourceDb::SOURCE_DB, > :prompt => "Select a Database" > %> > </p> > <%= submit_tag "Continue", :class => "submit" %> > <% end %> > </div> > > ------------------------------------------------------------------------------------------------- > <!-- > ! choose_table.html.erb > --> > <div class="choosetable-form"> > > <%= error_messages_for ''table'' %> > <legend>Please Choose a SWOT Table:</legend> > <% form_for :table, :url => { :action => :profile } do |form| %> > <p> > <label for="type"></label> > <%> form.select :type, > SelectTable::SELECT_TABLES, > :prompt => "Select a Table" > %> > </p> > <%= submit_tag "Continue", :class => "submit" %> > <% end %> > </div> > > ------------------------------------------------------------------------------------------------- > <!-- > ! profile.html.erb > --> > <div class="box"> > <% form_tag :action => :profile do %> > <label for="client_id">Client ID</label> > <%= 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 %> > </div> > > <div id="list"> > <h1>USER <%="#{@db_env}" %>List</h1> > > <table cellpadding="5" cellspacing="0"> > <tr class="list-name"> > <th>Client Id</th> > <th>Field 2</th> > <th>Field 3</th> > <th>Field 4</th> > <th>Field 5</th> > <th>Field 6</th> > <th>Field 7</th> > <th>Field 8</th> > <th>Field 9</th> > <th>Field 10</th> > </tr> > > <% 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 %> > > </table> > </div> > <div class="digg_pagination"> > <div clas="page_info"> > <%= page_entries_info @users %> > </div> > <%= will_paginate @users, :container => false %> > </div> > ------------------------------------------------------------------------------------------------- > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---