0

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.

4

1 回答 1

2

你可以这样做:

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

在控制器中:

@object.set_visibility(params[:visible])

这是你的意思吗?

于 2013-05-29T10:13:53.053 回答