如果操作是编辑,则不显示表单字段 [英] Don't display field of form if action is edit

查看:44
本文介绍了如果操作是编辑,则不显示表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rails 表单,它使用字段列表呈现部分以创建新用户.我也用来呈现编辑表单的那些字段.我想知道是否可以在编辑表单上不显示特定字段(即部门),因为我不想让用户选择更改部门?

I have rails form that renders partial with fields list to create new user. Those fields I am also using to render edit form. I wonder if it is possible not to show specific field (i.e. department) on the edit form, because I don't want to give users option to change the department?

推荐答案

可以使用 persisted? 方法来确保不会插入新记录甚至使用 new_record? 方法,如果记录未保留在您的数据库新记录"中,则该方法将返回 true.例如::

You can use persisted? method to ensure that it is not a new record that will be inserted or even use new_record? method which will return true if the record is not persisted in your database "New record". ex ::

<%= f.text_field :department if @user.persisted? %>
 or 
<%= f.text_field :department unless @user.new_record? %>

另一种方法是检查操作本身,如果您从不同的视图渲染部分并且希望将其限制为特定的控制器,这也可以让您控制控制器,这些存储在 ::

one more way is to check about the action itself which also gives you control on the controller if you render the partial from different view and you want to limit it to specific controller, these are stored in ::

params[:controller] -->> contains the name of the controller ex. UserController  that you just hit on
params[:action] -->> contains the action name ex. new or edit

这篇关于如果操作是编辑,则不显示表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆