views:

1710

answers:

3

Hi, I'm attempting to pass variables between forms and unfortunately i'm not managing

in my index.html.erb, i have a text field named SearchInput and i would like to pass this variable to the next form search.html.erb so that i may populate some data accordingly. How may i go about this please?

In my index.html.erb i have the following <% form_tag (:action => 'search') do %>

I'm really clueless on how to go about on this please, any tips would be really appreciated

+2  A: 

Form data is passed as part of the params[..] hash. So you can access most of the data passed through a form in that manner.

In your case, your view

<form action="..." method="...">
  <input type=".." name="search_input" ... />
  ...
  ...
  <button>submit</button>
</form>

would be grabbed via, your controller

def action_name
  @collection = Model.find(:conditions => {:column_name => params['search_input']})
  ...
  ...
end
nowk
im afraid i'm really not understanding this being very new to ruby on rails..in which files to i put which please? :s
Erika
I expanded the example a bit, hope that helps.
nowk
+3  A: 

It sounds like you could benefit from reading up on the interactions between Model, View and Controller in a MVC framework like Rails. I suggest the official Getting Started Guide

Here's the relevant details as it relates to your question:

When receiving a HTTP request Rails will route the request to a controller action based on the url path. The controller than builds the appropriate response by evaluating view templates in the context of the request. The standard flow in Rails is the controller will extract information from the params hash to query the database for the information required to construct the view. Instance values set in the controller (variables with names starting with '@') will be available to the views rendered by this controller during this action.

Forms in general, direct the client's web browser to send a POST request to the specified url when the submit button is clicked. This request contains the form's fields and their values as data in the header.

Rails interprets the form data and any other parameters in the params hash. Which you can access in the controller or view of any action. The generally accepted place to access the params hash is in the controller.

Lets walk through the process of what's actually happening. The following is in terms of running a server in development assuming we're talking about a controller for products, and you've set up routes correctly.

  1. User enters http://localhost:3000/products into their address bar to initiate a GET request.

  2. Rails routes this request to the index action of products controller.

  3. Rails will execute ProductsController#index (the index method located in app/controllers/products_controller.rb)

  4. Unless you explicitly render or redirect the request, Rails will render the products/index template (app/views/products/index.html.erb) which contains a small form that submits to the search action of ProductsController. Form code will look like this:

    <% form_tag(:action => 'search') do %>
      <%= text_field_tag "SearchInput" %>
      <%= submit_tag %>
    <% end %>
    
  5. User fills in the SearchInput field and submits the form as a POST request to http://localhost:3000/products/search

  6. Rails routes this request to the search action of the products controller.

  7. Rails executes ProductsController#search, the params hash contains the value the user entered into the SearchInput field with the SearchInput key. Here's a very simple example of using the form input to perform a search.

    def search
      @products = Product.find_by_name(params["SearchInput"])
    end
    
  8. Unless you explicitly render or redirect the request, Rails will render the products/search template (app/views/products/search.html.erb) Example search view:

    <% if @products.empty? %>
      No products found
    <% else %>
      <% @products.each do |product| %>
        <%= link_to product.name, product_url(product)%> 
        <br />
      <% end %>
    <% end %>
    
EmFi
this was extremely useful, thanks so much for the detailed explanation
Erika
No problem, just remember the search action in the solution is very simple and will only find an exact match. In practice you'll want to use a proper search plugin like ferret, sphinx, solr, xapian, etc.
EmFi
A: 

i have a variable of type openstruct in my controller. and i want to access the values in this structure in my view.the following is the code snippet.

yield Result[:successful], identity_url, OpenID::SReg::Response.from_success_response(open_id_response)

    @user = OpenStruct.new
    @user.identity_url = identity_url
    @user.nickname = open_id_response[:nickname]
    @user.email = open_id_response[:email]
      session[:user_id] = @user
      redirect_to(:controller=>'user',:action=>'private')

according to me the attributes nickname,email should be accessible in 'private'.. but i keep getting an error ArgumentError in UserController#login nickname is not a defined simple registration field

and the following respnse:

Request

Parameters:

{"openid.ax.type.gender"=>"http://axschema.org/person/gender", "openid.mode"=>"id_res", "openid.return_to"=>"http://localhost:3000/user/login?open_id_complete=1", "openid.claimed_id"=>"https://me.yahoo.com/a/g1txs913gePSLaq_S5lJOX9sKXI-#25a64", "openid.ax.value.gender"=>"F", "openid.ax.type.language"=>"http://axschema.org/pref/language", "openid.sig"=>"qPOKg3HTT5Y7trfO8L9A1rnAfCg=", "openid.ax.type.nickname"=>"http://axschema.org/namePerson/friendly", "openid.ax.type.timezone"=>"http://axschema.org/pref/timezone", "openid.ns"=>"http://specs.openid.net/auth/2.0", "

openid.ax.value.nickname"=>"Prad",

"openid.ax.value.language"=>"en-IN", "openid.op_endpoint"=>"https://open.login.yahooapis.com/openid/op/auth", "openid.response_nonce"=>"2010-02-02T02:15:01ZcYSmqXcpqIIZDgYws87m9j7qt70CtCqQeQ--", "openid.ax.type.email"=>"http://axschema.org/contact/email", "openid.ax.type.fullname"=>"http://axschema.org/namePerson", "openid.ns.ax"=>"http://openid.net/srv/ax/1.0", "openid.ax.value.fullname"=>"Pradnya Shah", "openid.identity"=>"https://me.yahoo.com/a/g1txs913gePSLaq_S5lJOX9sKXI-", "open_id_complete"=>"1", "openid.ax.value.timezone"=>"Asia/Shanghai", "openid.pape.auth_level.nist"=>"0", "openid.assoc_handle"=>"ZUlkR97hj89gI6bD9pALWZo1YHM4uqS0bXyFs5O5QV_D_v__UNU7bNaX.eZvaG2RsOeSDzkFfIyNF1kWfAzu9OPUocaScG7PJHgRD8d0deEIVYvWgorxKvSxaq_k", "openid.signed"=>"assoc_handle, claimed_id, identity, mode, ns, op_endpoint, response_nonce, return_to, signed, ax.value.email, ax.type.email, ax.value.nickname, ax.type.nickname, ax.value.timezone, ax.type.timezone, ax.value.gender, ax.type.gender, ax.value.language, ax.type.language, ax.value.fullname, ax.type.fullname, ns.ax, ax.mode, pape.auth_level.nist", "openid.realm"=>"http://localhost:3000", "openid.ax.mode"=>"fetch_response", "openid.ax.value.email"=>"[email protected]"}

pradnya shah