RoR: why am I getting undefined method 'hidden_field_tag'?
Asked Answered
F

2

5

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?

Frottage answered 13/9, 2012 at 22:13 Comment(0)
L
12

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.

Luckin answered 13/9, 2012 at 22:43 Comment(6)
ok thanks. I noticed that when I create these microposts it doesnt record anything in the type column of the database. Does this mean that it did'nt work or does hidden_field_tag just not write to the database?Frottage
I've edited my answer to better explain what hidden_field_tag does. You need to make sure the name of the field is the name that will end up being part of the hash which is saved to the database.Luckin
then it gives me the error: can't mass-assign protected attributes: type. how is this mass assignmentFrottage
In your controller you are saving the record by using something like Micropost.create(params[:micropost]). That is "mass-assigning" the attributes contained under params[:micropost]. If you want to be able to mass assign attributes, you need to add those attributes to attr_accessible in the Micropost model. Add something like this: attr_accessible :typeLuckin
oh ok so the params are all of the columns in the database? so type for example is a param that has to be accessible to be assigned when the micropost is created?Frottage
Yes. Keep in mind this is only for mass assignment like Micropost.create(params[:micropost]). If you did something like this, it would not be considered mass-assignment: micropost = Micropost.new; micropost.type = params[:micropost][:type];Luckin
M
2

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

Mash answered 13/9, 2012 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.