[英]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 %>
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.