简体   繁体   English

如何在Rails中自定义强参数

[英]How to customize strong parameter in rails

i'm a rails newbie and I got a problem. 我是Rails新手,但遇到了问题。 When I user a strong parameter: 当我使用强参数时:

params.require(:project).permit(:project_name, :real_time)

I want to use it do update the project: 我想用它来更新项目:

@project.update_attributes(user_project_params)

but before that, i want to change the value of 但在此之前,我想更改

:real_time
ex: :realtime += 2

please tell me how can i do that? 请告诉我我该怎么做?

Before calling user_project_params update the params 在调用user_project_params之前,请更新参数

  params[:project][:real_time] += 2

Otherwise use callbacks 否则使用回调

Any predefined modifications to the model objects are always done in model. 对模型对象的任何预定义修改总是在模型中完成。

You can write a method in model and call it using callbacks 您可以在模型中编写方法并使用回调进行调用

class Project< ActiveRecord::Base
  before_save :update_real_time

  private
    def update_real_time
      self.real_time= self.real_time +2
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM