为什么要在 rails 中使用 _form.html.erb [英] Why to use _form.html.erb in rails
问题描述
我是 Ruby on Rails 的新手.我使用脚手架为 admin
现在我的一位前辈告诉我在 _form.html.erb
中为管理员进行更改.我不知道如何实现它.还有使用/制作的优点或缺点是什么_form.html.erb 与普通方法相比的变化(更改索引、新建、显示和编辑)
我的_form.html.erb如下:-
<%= form_for(@admin) do |f|%><% 如果@admin.errors.any?%><div id="error_explanation"><h2><%=pluralize(@admin.errors.count, "error") %>禁止保存此管理员:</h2><ul><% @admin.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><div class="actions"><%= f.submit %>
<%结束%>
我的管理表如下:-
id 序列号非空,用户名字符变化(20)NOT NULL DEFAULT ''::character变化,电子邮件字符变化(255),first_name 字符变化(100),中间名字符变化(100),姓氏字符变化(100),指定字符变化(255),office_phone 字符变化(255),已删除整数 NOT NULL DEFAULT 0,super_admin boolean NOT NULL DEFAULT false,created_at 时间戳不带时区 NOT NULL,无时区的updated_at 时间戳NOT NULL,约束 admins_pkey PRIMARY KEY (id)
谁能帮我实现这个.提前致谢
文件以_"下划线开头,在 rails 中称为部分.它鼓励DRY(不要重复自己)软件开发的原则.您可以重用部分,在这种情况下 _form.html.erb 可用于 new.html.erb 和 edit.html.erb 功能.我们可以在其他 html.erb 文件中渲染部分或在控制器中调用.render :partial =>'partial_name'
在partial_name 中没有下划线.
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
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.
这篇关于为什么要在 rails 中使用 _form.html.erb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!