[Rails] View sharing

View sharing

Set views with the same input contents such as edit and new together. The following 3 files will be prepared this time, and edit and new will have a common input form. _form.html.erb edit.html.erb new.html.erb

If you write form_for (@profile) in the shared form, routing will be done automatically. In addition, the notation of the submit button is also automated, and if it is translated into Japanese, it will be written as register and update.

_form.html.erb Describe the form to share.

_form.html.erb


<%= form_for(@profile) do |f| %>

<div class="field">
  <%= f.label :img %><br />
  <%= f.file_field :img %>
  <%#The error content is displayed in red below%>
  <% if @profile.errors.include?(:img) %>
  <p style="color: red;"><%= @profile.errors.full_messages_for(:img).first %>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
      <%#The error content is displayed in red below%>
      <% if @profile.errors.include?(:name) %>
      <p style="color: red;"><%= @profile.errors.full_messages_for(:name).first %>
        <% end %>

    </div>
    <div class="field">
      <%= f.label :age %><br />
      <%= f.number_field :age, autofocus: true, autocomplete: "age" %>
      <%#The error content is displayed in red below%>
      <% if @profile.errors.include?(:age) %>
      <p style="color: red;"><%= @profile.errors.full_messages_for(:age).first %>
        <% end %>
    </div>

    <div class="field">
      <%= f.label :male%><%= f.radio_button :sex, :male%>
      <%= f.label :Female%><%= f.radio_button :sex, :Female%>
      <%#The error content is displayed in red below%>
      <% if @profile.errors.include?(:sex) %>
      <p style="color: red;"><%= @profile.errors.full_messages_for(:sex).first %>
        <% end %>
    </div>

    <div class="field">
      <%= f.label :description %><br />
      <%= f.text_area :description, autofocus: true, autocomplete: "description" %>
      <%#The error content is displayed in red below%>
      <% if @profile.errors.include?(:description) %>
      <p style="color: red;"><%= @profile.errors.full_messages_for(:description).first %>
        <% end %>
    </div>

    <div class="field">
      <%= f.label :qualify %><br />
      <%= f.text_area :qualify, autofocus: true, autocomplete: "qualify" %>
      <%#The error content is displayed in red below%>
      <% if @profile.errors.include?(:qualify) %>
      <p style="color: red;"><%= @profile.errors.full_messages_for(:qualify).first %>
        <% end %>
    </div>

    <div class="field">
      <%= f.label :impression %><br />
      <%= f.text_area :impression, autofocus: true, autocomplete: "impression" %>
      <%#The error content is displayed in red below%>
      <% if @profile.errors.include?(:impression) %>
      <p style="color: red;"><%= @profile.errors.full_messages_for(:impression).first %>
        <% end %>
    </div>

    <div class="actions">
      <%= f.submit %>
    </div>
    <% end %>

edit.html.erb Call the form to be shared by render and receive the variable from the controller with profile: @profile.

edit.html.erb


<h1>Profile edit</h1>

<%= render 'form', profile: @profile %>

new.html.erb Call the form shared by render.

new.html.erb


<h1>Profile registration</h1>
<%= render 'form' %>

Recommended Posts

[Rails] View sharing
View monthly calendar in Rails
Rails routing controller view relationship
Rails 5 Code Reading Part 2 ~ Action View ~
[Rails] "Input"-> "Confirmation screen"-> "Save"-> "View"
[Rails g.error]
Rails Review 1
Android view
Rails API
Rails migration
[Rails] first_or_initialize
[Ruby on Rails] View test with RSpec
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
Specify home view in rails app [root]
[Rails 6] Implementation of SNS (Twitter) sharing function
[Rails] devise
rails tutorial
rails tutorial
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
[Japanese localization] i18n rails Easy Japanese localization view display only
[Rails] Introducing pay.jp (from view customization to registration)