Abhiram
Abhiram

Reputation: 1489

Hidden_field_tag values are missing during submission of a form_for form

I am having a form using form_for helper, In this form I have included some hidden_field_tags which I am updating through javascript,

This is the form:

 <% form_for :ticket, @ticket, :url => { :controller => 'provider/tickets', :action => 'create'}, :html => { :id => "new_ticket", :multipart => true } do |f| %>
 <%= f.error_messages %>
 <p>
  <%= f.label :Regarding %><br />
  <%= f.text_field :title %>
  <%= hidden_field_tag :general_comments, '', :id => "general_comments" %>
  <%= hidden_field_tag :resident_id, @resident.id %>
  <%= hidden_field_tag :send_type, 'everyone', :id => "send_type" %>
  <%= hidden_field_tag :email, true, :id => "email_value" %>
  <% @categories.each do |category_comments_|%>
  <%= hidden_field_tag "category_comments_" + category_comments_.id.to_s , '', :id => "category_comments_" + category_comments_.id.to_s %> 
 <% end %>
 </p>
 <%= f.submit 'Create' %>
 </p>
 <% end %>

I am updating the hidden_field_tag through javascript and it is getting assigned with values when I look in the browser,

Value got assigned to the hidden values

But if I submit the form the params of this hidden_field_tag are empty, I dono why this is happening, please check the below screenshot to see the empty parameter while submitting.

enter image description here

But if I use the same fields in form_tag it is working fine, if i use it in form_for it is not working.

The values inside this loop is only not submitting properly,

   <% @categories.each do |category_comments_|%>
    <%= hidden_field_tag "category_comments_" + category_comments_.id.to_s , '', :id => "category_comments_" + category_comments_.id.to_s %> 
   <% end %>

Since the app is too old, the version is Ruby 1.8.7 and Rails 2.3. Can anyone help me to solve this.

Thanks.

Upvotes: 1

Views: 1769

Answers (2)

Abhiram
Abhiram

Reputation: 1489

I found the problem,

I have used string instead of :symbol in the name of the hidden_field_tag. Now I changed the string to symbol and now all the values are sent with params while submitting the form.

This is what i already had,

<% @categories.each do |category_comments_|%>
<%= hidden_field_tag "category_comments_" + category_comments_.id.to_s , '', :id => "category_comments_" + category_comments_.id.to_s %> 
<% end %>

Now I changed this like below,

<% @categories.each do |category_comments_|%>
         <% specific_category = "category_comments_" + category_comments_.id.to_s %>
  <%= hidden_field_tag specific_category.parameterize.underscore.to_sym, '', :id => "category_comments_" + category_comments_.id.to_s %>
<% end %> 

Now it works fine.

Upvotes: 0

aldrien.h
aldrien.h

Reputation: 3635

Based on the documentation: http://apidock.com/rails/ActionView/Helpers/FormHelper/hidden_field

You can add a value to your hidden field by using the :value parameter.

Example:

hidden_field(:object, :field, :value => params[:requestval])

When updating values via javascript, you may need to set like:

f.hidden_field :field_name, {:value => ''}

As, they suggested in Hidden Field Submission

Upvotes: 2

Related Questions