2014-11-08 47 views

回答

1

如果PARAMS [:行动]为编辑则不会显示该字段可能是通过使用显示器没有

+0

我试过,但该领域的后来不同时行动,新的编辑:( – 2014-11-08 12:18:57

+0

但在新的情况下..行动将是新的,然后不能编辑字段应该显示出。如果其他条件显示字段使用其他情况下隐藏它 – 2014-11-08 18:10:21

1

它可以通过多种方式来完成。

我通常不喜欢它

if [email protected]_record? 
# department field. 
end 
+0

它不起作用,但我没有得到的部分new_record?我应该在我的userController for new_record有一个方法?或者它是一些内置的导轨方法?内置的 – 2014-11-08 12:18:22

+0

,'@ user'是一个AR对象 – Nithin 2014-11-08 12:29:49

2

只要把你的形式:

<%= f.text_field :department unless @user.new_record? %> 
9

您可以使用坚持?方法确保它不会插入新记录,甚至使用new_record?如果记录没有保存在数据库“新记录”中,它将返回true。 前::

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

另一种方式是检查有关的行动本身,这也给了你控制的控制器上,如果您呈现来自不同的看法偏,你想将其限制在特定的控制器,这些被存储在::

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 
相关问题