I'm new to rails, i'm trying to create an action for updating an existing record with some required fields. Right now i got something like that:
def assembling_params
params.require(:device).permit(:assembling_time, :operator_assembling_name)
end
Right now it check if my device param is present and whitelist only 2 attributes and it's working. But how can i do to put these 2 attributes required by my action ? (I don't want to put it in the model validation since these attributes aren't present when i create my object) Thanks
You can specify as many requires as you please.
This may look ugly, but it does the job.
def assembling_params
device_params = params.require(:device).permit(:assembling_time, :operator_assembling_name)
device_params.require(:assembling_time)
device_params.require(:operator_assembling_name)
device_params
end
Use in appropriate controller:
before_filter :validate_params
And method:
def required_params
[:some_attribute, :another_attribute]
end
def validate_params
unless (require_params - params.keys).count.zero?
# do something
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.