DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

Form tags of the ActionView::Helpers in Rails

πŸ”— Parent Note

At the first,

There are several ways to make form tag, so I was confused for a while.

But, because of rubyonrails.org, I somewhat understand them or can use them now. Thanks!

f.methods

If you're creating for a model or a form object, you use <% form_for: @person do |f| %> (ActionView::Helpers::FormBuilder). Then, this way is easy.

<% BLOOD_TYPE::Names.each |key, value| do %>
  <%= f.radio_button(:bload_type, key, {checked: key == f.objethe ct.bload_type, class: 'bload_type'}) %>
  <%= value %>
<% end %>

<%= f.text_area(:description, {class: 'myself', rows: 6, placeholder: "self intro"}) %>

Rails will make the HTML tags which id, names are made from the object attribute names, AUTOMATICALLY!.

Of course, you can make tags by following way, but you have to write id and names by your hands. It wastes time...

tag

If you are creating a form for NOT a model, just a form.

Maybe, you use <form_tag("/posts/#{@post.id}", method="post").

Then, you can't use f object.

It's like this.

<% BOOK::Categories.each do |key, value| %>
  <%= radio_button_tag(
        'book[category]',
        key,
        (key == f.object.category),
        class: 'my-class'
      ) %>
  <%= value %>
<% end %>

Top comments (0)