Forum: Ruby on Rails Magic variable: params and :id

Posted by Hanfei S. (hanfei_s)
on 2012-11-28 16:46
I enabled this line in `route.rb`

    match ':controller(/:action(/:id))(.:format)'

The HTML generated by `erb` is like this:

    <form method="post" action="/client_workouts/find"
accept-charset="UTF-8">
    <input type="hidden" value="✓" name="utf8">
    <input type="hidden"
value="UFX2eg089kb5Al3qcJXJvJEFYwFnkXCj+dsmXTYLszE="
name="authenticity_token">
    <input type="text" name="search_string" id="search_string">
    <input type="submit" value="Search" name="commit">
</form>


In the controller, it looks like this:


    def find
      puts params[:search_string]
    end



When I input some text in the text_field and click `Search`, the `find`
function can print out the value of that text.

This looks magic to me. Because in the `route`, the variable name is
`id`, but in the `controller`, this variable can be used by `params`. I
was wondering whether `id` is a magic variable that maps to `param`.


Does anyone have ideas about this. Thanks!
Posted by Walter Davis (walterdavis)
on 2012-11-28 17:40
(Received via mailing list)
On Nov 28, 2012, at 10:46 AM, Hanfei S. wrote:

> value="UFX2eg089kb5Al3qcJXJvJEFYwFnkXCj+dsmXTYLszE="
>      puts params[:search_string]
>
>
> Does anyone have ideas about this. Thanks!

In the controller, every parameter that was gleaned from the routing 
process is available in the params hash. This includes anything that was 
named in the route that matched the request, and POST or querystring 
(GET) variables. The only parts of the request that are not in that hash 
are the session and cookie parameters.

This gives you a one-stop-shop for any parameter you need, without 
having to specify in great detail precisely where you might have 
expected to find that parameter.

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