validation - Rails locking a 'completed' 'todo' -
i have todo model title:string, description:text
, completed:boolean
, 2 self references: children & parents joined todos_todos table (parent_id, child_id).
i want prevent todo completed == true
being edited unless user passes params[:completed] = false
. want prevent children being added todo if it's completed.
i know can in controller this:
def update ... @todo.find(params[:id]) if params[:completed] == false if @todo.completed == true render :edit ... end
...but i'm not sure that's correct way this. feel should use validation in model this, except can't figure out way compare user's input existing data in model.
just use authorization
(preferably cancancan
):
#gemfile gem "cancancan" #app/models/ability.rb class ability include cancan::ability def initialize(user) user ||= user.new # guest user (not logged in) cannot :manage, todo, completed: true end end #app/controllers/todos_controller.rb class todoscontroller < applicationcontroller laod_and_authorize_resource end
this prevent interactivity aforementioned records unless explicitly define in ability
class (maybe have admin
users can things etc).
Comments
Post a Comment