Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Rails:如何使文本字段只可编辑一次?_Html_Ruby On Rails - Fatal编程技术网

Html Rails:如何使文本字段只可编辑一次?

Html Rails:如何使文本字段只可编辑一次?,html,ruby-on-rails,Html,Ruby On Rails,我正在制作一个小表单,我试图做的是使其中一个文本字段仅在创建通知时获得值。我有: <%= f.label :User_ID %> <%= f.text_field :user_id, :value => current_user.id ,:readonly => true %> 当前_user.id,:readonly=>true%> 我的问题是,当其他用户编辑通知时,此文本字段会自动更改其值。我不知道该怎么做。谢谢 在您的视图中,您可以检查用户id是否

我正在制作一个小表单,我试图做的是使其中一个文本字段仅在创建通知时获得值。我有:

<%= f.label :User_ID %>
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>

当前_user.id,:readonly=>true%>

我的问题是,当其他用户编辑通知时,此文本字段会自动更改其值。我不知道该怎么做。谢谢

在您的视图中,您可以检查用户id是否已经存在,如果已经存在,则不显示用户id文本\u字段:

<% unless user_id.exists? %>  #only if user_id is not present, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
#仅当用户id不存在时,才显示文本字段
当前_user.id,:readonly=>true%>
另一个选项是向所引用的模型添加布尔值。例如,通知模型:

通知:字符串 编辑:布尔值(默认值:true)

用户创建通知后,可以将“编辑”的布尔值设置为false,例如使用After create

下次用户编辑该通知时,您将在视图中执行与以前相同的操作:

<% unless @notification.edit? %>  #only if edit is set to true, show the text_field
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
<% end %>
#仅当edit设置为true时,才显示文本字段
当前_user.id,:readonly=>true%>

它有点模糊,但它为您提供了一个方法。

添加一个布尔值进行编辑,更新一次后,将其更改为false。然后在您的模型中创建一个before_update过滤器,以阻止更改该字段。此示例允许客户端以任何方式更改当前_用户id(只需打开开发工具并编辑id)。不要相信那个人client@AlexanderLuna添加布尔值进行编辑是什么意思?谢谢!第二种方法非常适合我。我对这段代码所要做的就是修改,除非改为if,因为我得到了相反的效果。再次感谢你!