我如何只在两者中的一部分上显示链接?
好的,我有一个 Rails 表单部分,可以通过 2 个视图访问新的和编辑操作,但我只需要在编辑表单上显示一个链接,我正在尝试这种方法
新表单
= render :partial => 'form', :locals => {:f => f}
编辑表单
= render :partial => 'form', :locals => {:f => f, :show_link => true}
表单部分
if defined? show_link
有没有更好的方法实现这个或标准导轨方式是更好的方法
Ok so i have a rails form partial that is accessed by 2 views the new and edit action, but i only need to show a link on on edit form by i am trying this approach
new form
= render :partial => 'form', :locals => {:f => f}
edit form
= render :partial => 'form', :locals => {:f => f, :show_link => true}
form partial
if defined? show_link
Is there a better way to achieve this or a standard rails way that is a better approach
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它是链接而不是表单元素,那么通常更好的做法是使用嵌套渲染。换句话说,您有 3 个文件:
new.html
、edit.html
和_form.html
。new.html
和edit.html
都会渲染_form.html
,并且edit.html
包含额外的链接。如果需要,您可以设置链接元素的样式以显示在其他位置。如果链接更优雅地流入表单至关重要,请考虑使用
content_for
和yield
,以便edit.html
包括: >_form.html 然后包含
yield :extras
。注意:我故意省略了模板扩展,不知道您是否使用 erb、haml 或其他。
If it's a link and not a form element, it's generally better practice to have nested renders. In other words, you have 3 files:
new.html
,edit.html
, and_form.html
. Bothnew.html
andedit.html
render_form.html
, andedit.html
includes the extra link. You can style the link element to appear elsewhere if you need to.If it's crucial that the link flow more elegantly into the form, consider using
content_for
andyield
, so thatedit.html
includes:And
_form.html
then includesyield :extras
.NOTE: I deliberately left off template extensions, not knowing whether you're using erb, haml, other.
我之前见过的方法是使用
new_record
和persisted
,它们是彼此相反的。persisted
用于已保存的记录,new_record
用于未保存的记录。我将猜测这里的场景,但它是类似“在编辑页面上显示删除链接”吗?
如果是这样,我会做这样的事情(假设@object是表单的对象):(
新建和编辑表单)
(表单部分)
(示例位于haml中,但如果需要,应该很容易转换为erb)
The way I have seen this done before is using
new_record
andpersisted
, which are inverses of each other.persisted
is for saved records, andnew_record
is for unsaved.I'm going to guess at the scenario here, but is it something like 'show a delete link on the edit page'?
If so, I would do something like this (assuming @object is the object for the form):
(new and edit form)
(form partial)
(examples are in haml but should be easy to convert to erb if necessary)
您甚至不需要从编辑中传递
参数show_link
,您可以在表单视图
中检测操作
本身。在你可以使用的两个地方
,在
formpartial
中,你可以像链接是 html 标签一样这样做,你可以这样做
,所以现在它将检查
render
是否发生通过edit
操作,将添加link
。you need not even pass the
parameter show_link
from edit, you candetect the action
itself inform view
.in both the places you can use
and in
form partial
you can do it likeif link is html tag, you can do it like
So now it will check if the
render
happened fromedit
action, thelink
will be added.我通常会做一些类似的事情
来区分 Rails 表单中的
new
和edit
特定内容I usually do something like
to differ the
new
andedit
specific things in my rails forms