简体   繁体   English

为什么在 Rails 中使用 _form.html.erb

[英]Why to use _form.html.erb in rails

I am new to Ruby on Rails.I was using scaffold to create models views and controllers for admin我是 Ruby on Rails 的新手。我使用脚手架为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)现在我的一位前辈告诉我要为管理员更改_form.html.erb 。我不知道如何实现它。与 _form.html.erb 相比,使用/进行更改的优点或缺点普通方法(更改索引、新建、显示和编辑)

My _form.html.erb is as follows:-我的 _form.html.erb 如下:-

<%= 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有人可以帮我实现这个。在此先感谢

File Starts with "_" underscore called partials in rails.文件以“_”下划线开头,在 rails 中称为部分。 It encourages DRY (Do not repeat yourself) principle of software developement.它鼓励软件开发的DRY (不要重复自己)原则。 You can reuse partials and in this case _form.html.erb can be used for both new.html.erb and edit.html.erb functionality.您可以重用部分,在这种情况下,_form.html.erb 可以用于 new.html.erb 和 edit.html.erb 功能。 We can render partials in other html.erb files or called within the controllers too.我们可以在其他 html.erb 文件中渲染部分内容,也可以在控制器中调用。 render :partial => 'partial_name' without underscore in the partial_name. render :partial => 'partial_name'在 partial_name 中不带下划线。

It means you only need to make changes in one place, this is in keeping with Don't Repeat Yourself (DRY) in coding.这意味着您只需要在一个地方进行更改,这与编码中的 Don't Repeat Yourself (DRY) 一致。

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.要将其包含在 index、edit 等的 html 中,请插入render 'partial' ,其中 partial 是不带下划线的名称。

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

Read up on partials.阅读部分内容。 They help you DRY up your code.他们帮助你干掉你的代码。 Read more here 在这里阅读更多

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.在 Rails 中,通用视图代码可以放在名为 partials 的文件中,(阅读@Sachin R's回答),partials 基本上可以避免代码重复并保持代码干净/可维护。

In your case, when you run the scaffold , your new and edit views are using the same form.在您的情况下,当您运行 scaffold 时,您的new视图和edit视图使用相同的表单。 Because of that reason that form has converted to a partial ( _form ).由于这个原因,表单已转换为部分( _form )。 So when you do the changes in _form partial, the changes will be available for both new and edit views.因此,当您在_form partial 中进行更改时,这些更改将可用于new视图和edit视图。 (that's why your seniors have asked to do the changes in _form ), (这就是为什么你的前辈要求在_form中进行更改),

So the bottom line is, since partials are the common code segments, once you change them it will apply across the site.所以底线是,由于部分是公共代码段,一旦你改变它们,它将适用于整个站点。

You can add a _form.html.erb in /views/candidates , and copy the same contents in other different XXX.html.erb into _form.html.erb , then key <%= render 'form' %> in the XXX.html.erb .您可以在/views/candidates中添加一个_form.html.erb ,并将其他不同的XXX.html.erb中的相同内容复制到_form.html.erb中,然后在XXX.html.erb中键入<%= render 'form' %>XXX.html.erb It will help you render the partial contents into XXX.html.erb in order to simplify your code.它将帮助您将部分内容呈现为XXX.html.erb以简化您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails是否使用方法:: patch与_form.html.erb进行编辑? - Does rails use method: :patch for editing with _form.html.erb? _form.html.erb中的Ruby on Rails支架错误 - Ruby on rails scaffold error in _form.html.erb _form.html.erb中的嵌套form_for - Nested form_for in _form.html.erb 为什么 form_for(@object) 做 |f| 在 _form.html.erb 中工作但不在 index.html.erb 中工作 - Why does form_for(@object) do |f| work in the _form.html.erb but not in the index.html.erb 将rails从_form.html.erb移至application.html.erb - Moving rails from to from _form.html.erb to application.html.erb _form.html.erb中未定义的方法&#39;pluralizer&#39; - undefined method `pluralizer' in _form.html.erb 我的_form.html.erb无法使用Javascript吗? - Javascript is not working with my _form.html.erb? 在Ruby on Rails 5中验证失败后重新呈现_form.html.erb时失败 - Failure when re-rendering _form.html.erb after failing validation in Ruby on Rails 5 在_form.html.erb中将字段类型从复选框更改为单选按钮(Ruby on Rails 3) - Changing field type from check box to radio button in _form.html.erb (Ruby on Rails 3) 任何在 _form.html.erb 中生成单选按钮的 Rails 脚手架数据类型? - Any Rails scaffold data types that generate radio buttons in the _form.html.erb?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM