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
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.
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.
Forms like search forms don't need a database, so use form_tag.
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