Ruby on Rails
UnderstandingRequestParameters

Parameters1

All request parameters (GET or POST) are available through the @params hash. So an action that was performed through /weblog/list?category=All&limit=5 will include { "category" => "All", "limit" => 5 } in @params.

It’s also possible to construct multi-dimensional parameter hashes by specifying keys using brackets, such as:


  <input type="text" name="post[name]" value="david">
  <input type="text" name="post[address]" value="Hyacint Avenue 12">

A request stemming from a form holding these inputs will include
<pre> { "post" => { "name" => "david", "address" => "Hyacint Avenue 12" } } </pre>

If the address input had been named post[address][street], the @params would have included

<pre> { "post" => { "address" => { "street" => "Hyacint Avenue 12" } } } </pre>

There is no limit to the depth of the nesting.

1 from the “API documentation on "ActionController::Base”:http://api.rubyonrails.com/classes/ActionController/Base.html

Inputting arrays without explicit indices

In PHP, for example, it is possible to use empty brackets as in the following example to end up with array('<a href="mailto:workexample.com">work@example.com‘, ’personal@example.com’, ‘spam-me@example.com’)@ in $_REQUEST['email'].

<pre> <input name="email[]" type="text" value="<a href="mailto:work@example.com">work@example.com</a>" /> <input name="email[]" type="text" value="<a href="mailto:personal@example.com">personal@example.com</a>" /> <input name="email[]" type="text" value="<a href="mailto:spam-me@example.com">spam-me@example.com</a>" /> </pre>

That this works seems only logical, because in PHP array[] syntax is used to push new elements to an array.

However, this is not possible in Rails, so you’ll have to improvize by inputting arrays with explicit indices. Although it must be admitted that the PHP notation is very convenient, you can do the same thing and more with explicit indices in Rails.

Inputting arrays with explicit indices

Sequential indices

When you’re not rendering list items one at the time using AJAX, the simplest method of getting sensible indices is the following:

<pre> <% @projects.tasks.each_with_index do |x, task| %> <%= text_field_tag "tasks[#{x}][name]", task.name %> <%= text_field_tag "tasks[#{x}][description]", task.description %> <% end %>

Do note that the indices are, besides good looking, quite meaningless.

When you are adding your list items one at the time and you still want to have sequential indices, there are still some options.

One is based on the assumption that the next index is the new size of the list -1.

<pre> class ProjectController def add_task @project = Project.find @params['id'] new_task = @project.tasks.build @params['new_task'] new_task_idx = @project.tasks.size - 1

render :inline => text_field_tag( “tasks[#{new_task_idx}][name]”, new_task.name) + text_field_tag “tasks[#{new_task_idx}][description]”, new_task.description) end

end

itodd’s Way

he does it this way: let’s say the querystring is: ?address[0][address]=123+Fake+Street&address[1][address]=742+Evergreen+Terrace the params[:address] object will be a hash:

{{"0" => {"address" => “123 Fake Street”}, “1” => {"address" => “742 Evergreen Terrace”}}

as long as the hash keys are unique, you’re good to go, as params[:address].values = [ {"address" => “123 Fake Street”}, {"address" => “742 Evergreen Terrace”}]

Dummy indices

When you’re adding list items one at the time using AJAX, you’ll want to uniquely distinguish

GianniJacklone uses Time.now.to_s when he is in the need of incrementing indices in AJAX where the actual index value is meaningless. The advantage of this approach is that you can be sure to get a different index with each call to Time.now.

BigSmoke now uses Time.now.to_f.to_s

Polish up your params fu

Here is an article that describes 5 ways you can use Rails Controller params effectively.

Boost your Rails Controller params[:fu]


Screencast based on Boost your Rails Controller params fu

category: Understanding, Stub