Trancey
Trancey

Reputation: 709

Generate parameters in ruby on rails

I want an update method in the controller to get parameters in a specific format. The current format by which I am getting the parameters is

Parameters: {"utf8"=>"✓", "authenticity_token"=>"temp", "performance_areas"=>{"0"=>{"performance_area"=>"New item"}, "1"=>{"performance_area"=>"Delete Item"}, "commit"=>"Submit"}

This is being generated as a name in my text field with 0 and 1 indicating the areas index. I want some additional parameters in this in the following format:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"temp", "performance_areas"=>{"0"=>{id: 1, description: "This is a test","performance_area"=>"New item"}, "1"=>{id: 2, description: "This is a test2","performance_area"=>"Delete Item"}, "commit"=>"Submit"}

Any pointers how to get the parameters in the way I want. I do have performance_areas that has the id and description in it. Do let me know if any further clarification is required.

EDIT: Form code (just pasting the critical section of the long code)

<%= form_tag({:performance => :update}, :method => :put) do %>
<% @performance_areas.each_with_index do |performance_area, i| %>
<%= hidden_field_tag :performance_area, performance_area, :name => "performance_areas[#{i}][performance_area]" %>
<%= submit_tag %>

Upvotes: 2

Views: 73

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

I think you want this, I am assuming that both id and description are in performance_area object

<%= form_tag({:performance => :update}, :method => :put) do %>
  <% @performance_areas.each_with_index do |performance_area, i| %>
    <%= hidden_field_tag :id, performance_area.id, :name => "performance_areas[#{i}][id]" %>
    <%= hidden_field_tag :description, performance_area.description, :name => "performance_areas[#{i}][description]" %>
    <%= hidden_field_tag :performance_area, performance_area, :name => "performance_areas[#{i}][performance_area]" %>
    <%= submit_tag %>
<% end %>

Upvotes: 3

Related Questions