开发者

How can I deal with multiple text_fields mapped to a single parameter in Rails?

开发者 https://www.devze.com 2023-03-13 12:15 出处:网络
For instance, I have a table of text_fields like this: Entry 1 | Value (with flag=TRUE) | Value(with flag=FALSE)

For instance, I have a table of text_fields like this:

Entry 1 | Value (with flag=TRUE) | Value(with flag=FALSE)

Entry 2 ...

.

.

.

I need to be able to assign the "Value" whether it is in the left or right hand column (and set the corresponding flag).

Then on that same row if one column has an entry, then the other column should be grayed out (otherwise开发者_如何学运维 it would overwrite the other one).


I'd do this with extra, non-DB attributes on the model. Something like this:

class MyModel < ActiveRecord

  attr_accessor :val1, :val2

  def val1=(value)
    self.real_value = value  # Make sure your real database column is updated
    self.the_flag = true
  end

  def val1
    the_flag ? real_value : nil  # Return real database column when asked
  end

  def val2=(value)
    self.real_value = value  # Make sure your real database column is updated
    self.the_flag = false
  end

  def val2
    the_flag ? nil : real_value  # Return real database column when asked
  end

Then in your view, you hook up to val1 and val2 instead of the real column and use your flag to determine what's grayed out.

0

精彩评论

暂无评论...
验证码 换一张
取 消