DEV Community

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

Posted on

2 1

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 %>

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit