Reputation: 117
I'm making a little form and what I'm trying to do is to make one of the text fields get the value only when notification is created. I have:
<%= f.label :User_ID %>
<%= f.text_field :user_id, :value => current_user.id ,:readonly => true %>
My problem is that when other users are editing notifications, then this text field is automatically changing it's value. I'm not sure how to make it work. Thanks!
Upvotes: 1
Views: 511
Reputation: 5449
In your view you could check if the user id is already present and if so, don't display the user id text_field:
<% 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 %>
Another option would be to add a boolean value to the model you are referring. For example, a notification model:
notification: string edit: boolean (default: true)
After a user creates a notification, you set the boolean value for "edit" to false with an after create for example.
The next time a user edits that notification, you do the same in the view as before:
<% 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 %>
It is a little vague but it gives you an idea how to do it.
Upvotes: 1