【问题标题】:Rails 5 , render a form without specific inputRails 5,在没有特定输入的情况下呈现表单
【发布时间】:2016-09-05 12:35:14
【问题描述】:

我正在尝试渲染部分表单以进行编辑,但我不希望该表单的特定部分显示在编辑操作中。我正在使用普通格式渲染。

<%= render 'form', link: @link %>

我不想显示特定的部门

<div class="form-group">
<%= f.label :Description %><br>
<%= f.text_area :description, class: "form-control" %>

谢谢,

【问题讨论】:

  • link.persisted? 如果为真,则表示您正在编辑它,如果为假,则为新链接。

标签: ruby-on-rails ruby simple-form-for


【解决方案1】:

您可以通过以下方式实现:仅当对象是新记录时才显示此 div

<% if f.object.new_record? %>
  <div class="form-group">
    <%= f.label :Description %><br>
    <%= f.text_area :description, class: "form-control" %>
  </div>
<% end %>

另一种方法是将额外的参数传递给部分:

<% if allow_description %>
  <div class="form-group">
    <%= f.label :Description %><br>
    <%= f.text_area :description, class: "form-control" %>
  </div>
<% end %>

然后:

# new action
<%= render 'form', link: @link, allow_description: true %>

# edit action
<%= render 'form', link: @link, allow_description: false %>

【讨论】:

    【解决方案2】:

    你可以像这样用if 包裹 div

    <% if @link.new_record? %>
      <div class="form-group">
      <%= f.label :Description %><br>
      <%= f.text_area :description, class: "form-control" %>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多