Why to use _form.html.erb in rails

2019-07-20 02:45发布

问题:

I am new to Ruby on Rails.I was using scaffold to create models views and controllers for admin

Now one of my seniors has told me to make changes in _form.html.erb for admin.I have no idea how to implement it.Also what is the advantage or disadvantage of using/making changes in _form.html.erb as compared to normal method(make changes in index,new,show and edit)

My _form.html.erb is as follows:-

<%= form_for(@admin) do |f| %>
  <% if @admin.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@admin.errors.count, "error") %> prohibited this admin from being saved:</h2>

      <ul>
      <% @admin.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

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

My Admin table is as follows:-

id serial NOT NULL,
  username character varying(20) NOT NULL DEFAULT ''::character varying,
  email character varying(255),
  first_name character varying(100),
  middle_name character varying(100),
  last_name character varying(100),
  designation character varying(255),
  office_phone character varying(255),
  deleted integer NOT NULL DEFAULT 0,
  super_admin boolean NOT NULL DEFAULT false,
  created_at timestamp without time zone NOT NULL,
  updated_at timestamp without time zone NOT NULL,
  CONSTRAINT admins_pkey PRIMARY KEY (id)

Can someone help me implement this.Thanks in advance

回答1:

File Starts with "_" underscore called partials in rails. It encourages DRY(Do not repeat yourself) principle of software developement. You can reuse partials and in this case _form.html.erb can be used for both new.html.erb and edit.html.erb functionality. We can render partials in other html.erb files or called within the controllers too. render :partial => 'partial_name' without underscore in the partial_name.



回答2:

It means you only need to make changes in one place, this is in keeping with Don't Repeat Yourself (DRY) in coding.

To implement it, you simply drag out the code you want to a partial (named with _).

To include it in the html of index, edit etc, you insert render 'partial', where partial is the name of it WITHOUT the underscore.

Specifically in your example, you can refer to each of the items for the form using the following pattern

<%= f.label :email %>
<%= f.text_field :email %>


回答3:

Read up on partials. They help you DRY up your code. Read more here



回答4:

Here I'm trying to give you an answer with the help of other answers,

In Rails common view codes can be put in files called partials, (read @Sachin R's answer about partials), partials will basically avoid code duplications and keep your code clean/ maintainable.

In your case, when you run the scaffold , your new and edit views are using the same form. Because of that reason that form has converted to a partial (_form). So when you do the changes in _form partial, the changes will be available for both new and edit views. (that's why your seniors have asked to do the changes in _form),

So the bottom line is, since partials are the common code segments, once you change them it will apply across the site.