I'm working on a Rails app and I have several actions( #delete_later, #ban_later and so on) where I only set one attribute from the request parameter( specifically, a reason
field for doing that action).
I was wondering if it is ok to do it like this:
def ban_later
@object.reason = params[:object][:reason]
@object.save
end
Or is it a best practice to use strong params even in this situation?
def ban_later
@object.reason = object_params[:reason]
@object.save
end
private
def object_params
params.require(:object).permit(:permitted_1, :permitted_2, :reason)
end
Which of these solutions is the best one? If none of them is, then what's the best solution to my problem?
Later Edit:
The #ban_later, #delete_later actions can indeed set a flag column status
but that can be done without receiving it's value from the params hash. Since you will only set one status per method you can simply set the status "pending_delete" when you are in #delete_later and "pending_ban" when you are in #ban_later.
Later Later Edit
Why use #save
and not update_attributes
directly? Let's say you need to have a if @object.save
statement. On the false branch( object not saved) you might still want to render a view where the contents of that @object
are used.
status
but that can be done without receiving it's value from the params hash. Since you will only set one status per method you can simply set the status "pending_delete" when you are in #delete_later and "pending_ban" when you are in #ban_later. – Advocaat