我如何只在两者中的一部分上显示链接?

发布于 2024-12-24 04:06:34 字数 387 浏览 0 评论 0原文

好的,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-12-31 04:06:34

如果它是链接而不是表单元素,那么通常更好的做法是使用嵌套渲染。换句话说,您有 3 个文件:new.htmledit.html_form.htmlnew.htmledit.html 都会渲染 _form.html,并且 edit.html 包含额外的链接。如果需要,您可以设置链接元素的样式以显示在其他位置。

如果链接更优雅地流入表单至关重要,请考虑使用 content_foryield,以便 edit.html 包括

content_for :extras do
    # link here
end

: >_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. Both new.html and edit.html render _form.html, and edit.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 and yield, so that edit.html includes:

content_for :extras do
    # link here
end

And _form.html then includes yield :extras.

NOTE: I deliberately left off template extensions, not knowing whether you're using erb, haml, other.

浅浅淡淡 2024-12-31 04:06:34

我之前见过的方法是使用 new_recordpersisted,它们是彼此相反的。 persisted 用于已保存的记录,new_record 用于未保存的记录。

我将猜测这里的场景,但它是类似“在编辑页面上显示删除链接”吗?

如果是这样,我会做这样的事情(假设@object是表单的对象):(

新建和编辑表单)

= render 'form'

(表单部分)

- if object.persisted?
  = link_to 'Delete', @object, :method => :delete

(示例位于haml中,但如果需要,应该很容易转换为erb)

The way I have seen this done before is using new_record and persisted, which are inverses of each other. persisted is for saved records, and new_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)

= render 'form'

(form partial)

- if object.persisted?
  = link_to 'Delete', @object, :method => :delete

(examples are in haml but should be easy to convert to erb if necessary)

も让我眼熟你 2024-12-31 04:06:34

您甚至不需要从编辑中传递参数show_link,您可以在表单视图检测操作本身。

在你可以使用的两个地方

<%= render :partial => 'form', :locals => {:f => f} %>

,在 formpartial 中,你可以像

<%= (link_to ........ ) if params[:action] == "edit" %>

链接是 html 标签一样这样做,你可以这样做

<% if params[:action] == "edit" %>
<a href="...">click me to edit</a>
<% end %>

,所以现在它将检查 render 是否发生通过 edit 操作,将添加 link

you need not even pass the parameter show_link from edit, you can detect the action itself in form view.

in both the places you can use

<%= render :partial => 'form', :locals => {:f => f} %>

and in form partial you can do it like

<%= (link_to ........ ) if params[:action] == "edit" %>

if link is html tag, you can do it like

<% if params[:action] == "edit" %>
<a href="...">click me to edit</a>
<% end %>

So now it will check if the render happened from edit action, the link will be added.

牵你手 2024-12-31 04:06:34

我通常会做一些类似的事情

if @record.new_record?
  # render 'new' partial
else
  # render 'edit' partial
end

来区分 Rails 表单中的 newedit 特定内容

I usually do something like

if @record.new_record?
  # render 'new' partial
else
  # render 'edit' partial
end

to differ the new and edit specific things in my rails forms

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文