In Rails edit action form, hide a form field [clos

2019-08-14 18:10发布

I have a Rails application wherein I don't want some of the form fields to appear in my edit action form.

How can I go about doing this in rails?

3条回答
Root(大扎)
2楼-- · 2019-08-14 18:57

One possibility is to use the persisted? function in ActiveRecord to detect if the form object is already persisted to the database, rather a new object.

<%= form_for @user do |f| ->

  <%- if @user.persisted? %>
    <%# Will only show if @user has been saved to the database %>
    <%= f.text_field :some_attribute_to_show_during_edit %>
查看更多
Explosion°爆炸
3楼-- · 2019-08-14 19:04

You have 3 common options to make fields that don't show up on the page:

  • Create hidden type input fields
  • Hide the fields with CSS
  • Omit the fields from the page

This answer describes all of these and how you might use them. Which works best in your specific case depends entirely on what you're trying to accomplish. My latest application uses all of these techniques, and sometimes all of them on the same page.

These aren't the only options, but this covers the most common requirements for hidden fields.

Hidden type input fields

HTML has a type of input field called "hidden" that allows you to keep data on the page that will be sent to the server with all other fields when the form is submitted. However, a hidden type field can never be changed into a displayed field; it's always hidden. You can do this like so:

<input type="hidden" id="my_id" name="my_name" value="my value">

With the Rails form helpers, you can use this tag to get the same effect:

<%= f.hidden_field :my_id, value: my.value %>

Or, you can use this tag if your data is not associated with the model object:

<%= hidden_field_tag :my_id, my.value %>

You can find more information at the [Rails Guides Form Helpers] 1 page in the Other Helpers of Interest section.

CSS styling to hide fields

When you create a field, you can use CSS styling to determine the field's initial visibility. With CSS styling, the field will not be visible to the user, but it will be sent to the server when the form is submitted.

You can use CSS styling to hide a field using the style="display: none;" attribute on the HTML element. Here's a simple HTML example:

<input type="text" style="display: none;" id="my_id" name="my_name">

With any of the Rails helpers for creating an input field, you can use the style: "display: none;" HTML attribute option, like this:

<%= f.text_field :my_field, style: "display: none;" %>

This is typically useful if you have dynamic Javascript behaviors that will later show the field when some event occurs, however, the technique is also useful simply to avoid displaying fields when you don't need them.

Omit fields from the page

This option is useful when you write conditional code in your Rails Views, so that some fields are sent to the rendered page, and some are not. Here's how to do that:

<%= form_for @my_model do |f| %>
  <% if some_condition %>
    <% f.text_field :my_field %>
  <% end %>
<% end %>

In this case, the my_field text field will only be included on the page if some_condition is true. This makes it easy to clean up complicated forms simply by leaving the unneeded components out when the view is rendered.

查看更多
神经病院院长
4楼-- · 2019-08-14 19:05

Why don't you just remove them from the view?

查看更多
登录 后发表回答