About ruby ​​form

form_for Methods that allow you to create form tags in Rails Form_for for user registration and information update function, form_tag for search function

form_for argument

Specify the model instance as an argument.


<%= form_for(@user) do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

In form_for, the path destination is not specified like form_tag.


def new
  @user = User.new
end

def edit
  @user = User.find(params[:id])
end

The instance created by the controller is created by the new method, and if it does not have the information, it is assigned to the create action, and if it already has the information, it is assigned to the update action.

How to create a post form with form_for

Describe as "f.html tag name: column name".


<%= form_for(@user) do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

In the above example, it is saved in the name column of the user table.

html tags that can be used in form_for

image.png

・ Form_tag

Forms like search forms don't need a database, so use form_tag.

・ Url option

You can specify the action when <% = form.submit%> is pressed.


<%= form_for @fruit, url: fruit_bulk_create_path do |form| %>
<%= form.label :name %> <%= form.text_field :name, id: :fruit_name %>
<%= form.label :description %> <%= form.text_area :description, id: :fruit_description %>
<%= form.submit "Bulk registration" %>
<% end %>

In the above code, when "form.submit" is executed, the "fruit # fruit_bulk_create" action is executed.

Recommended Posts

About ruby ​​form
About Ruby symbols
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About Ruby variables
About Ruby methods
About Ruby Kernel Module
About Ruby exception handling
About Ruby Hashes (continued)
About eval in Ruby
[ruby] About here documents
About Ruby if statement
About Ruby instance methods
[Ruby] About instance generation
About form. ○○ of form_with
About the [ruby] operator
Thinking about logic Ruby
Explanation about Ruby Range object
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
About Ruby hashes and symbols
Ruby About various iterative processes
About =
About Ruby and object model
About Ruby classes and instances
Explanation about Ruby String object
About the behavior of ruby Hash # ==
About Ruby single quotes and double quotes
About Ruby product operator (&) and sum operator (|)
[Super Introduction] About Symbols in Ruby
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
About method.invoke
Ruby learning 4
About Kotlin
About attr_accessor
From (form)
[Ruby] Array
About Hinemos
Ruby learning 5
About inheritance
Ruby basics
About Docker
Ruby Review 2
Ruby addition
Refactoring Ruby
About Rails 6
About form_for
About Spring ③
[Ruby on Rails] About bundler (for beginners)
Ruby learning 3
About enum
About polymorphism
About Optional
About hashes
About JitPack