Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR: why am I getting undefined method 'hidden_field_tag'?

right now I have two forms in a row

<section>
        <%= render 'shared/micropost_form_purchase' %>
        <%= render 'shared/micropost_form_sale' %>
      </section>

then for _micropost_form_purchase.html.erb

<%= form_for(@micropost) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field no-indent">
    <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
    <%= f.hidden_field_tag :type, :value => "purchase" %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

and for _micropost_form_sale.html.erb I have

<%= form_for(@micropost, :html => { :id => "sale" }) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field no-indent">
    <%= f.text_area :content, placeholder: "What's something else you want to buy?" %>
    <%= f.hidden_field_tag :type, :value => "sale" %>
  </div>
  <%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>

so I want the first micro post to automatically become a purchase micropost (I have a column in the micropost database called type that is a string that I want to depict either sale or purchase) and for the second one I want it to become a sale micropost. I was using hidden_field_tag because I thought you didn't have to define it in the controller, but am I wrong? Is hidden_field more appropriate? how can I use hidden_field_tag?

like image 962
BigBoy1337 Avatar asked Sep 13 '12 22:09

BigBoy1337


2 Answers

You can use:

<%= f.hidden_field :type, :value => "sale" %>

or:

<%= hidden_field_tag 'micropost[type]', "sale" %>

but not:

<%= f.hidden_field_tag :type, :value => "sale" %>

Using f.hidden_field will use the value from the variable @micropost, whereas hidden_field_tag will not use that.

like image 73
weexpectedTHIS Avatar answered Sep 28 '22 05:09

weexpectedTHIS


It should be f.hidden_field not f.hidden_field_tag as you're using the model's form helpers :)

like image 31
FluffyJack Avatar answered Sep 28 '22 04:09

FluffyJack