简体   繁体   English

Rails,是否可以将param从控制器传递给模型方法?

[英]Rails, Is it possible to pass param from controller to a model method?

In my models I have several similar named methods to enable/disable certain features, like: 在我的模型中,我有几个类似的命名方法来启用/禁用某些功能,例如:

  def invisible
    self.update_attribute(:invisble, false)
  end

  def visible
    self.update_attribute(:invisble, true)
  end

Would it be possible to refactor this to a single method and pass an argument from controller? 是否有可能将此重构为单个方法并从控制器传递参数? Or is there a better way to handle these issues. 或者有更好的方法来处理这些问题。

You can do something like this: 你可以这样做:

def set_visibility(param)
  self.update_attribute(:invisible, param == "true")
end

In controller: 在控制器中:

@object.set_visibility(params[:visible])

Is that what you meant? 这是你的意思吗?

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

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