0
votes

Ok, this should be on here already somewhere, but I can't find it.

I'm building a form with simple_form, and I'm using a dummy radio select input which values are based on a method (named 'simple?') in my 'Price' model. The methods looks if some attributes are used and returns false if they are. This way I can hide the advanced inputs in my form if they are not used. I want to use simple_form so the method is connected to the right object because more prices can be made (and created) in the same form. I use the radio buttons (and javascript) so users can still unhide the advanced fields.

Problem is when I submit the form, the form including the :simple? method is submitted, which returns errors because it is just a method.

Question is: How do I make sure the ':simple?' value for the is not submitted through the form, but is initiated to the right value? Or is there a smarter way of doing this?

form.haml

= f.input :simple?, as: :radio, label: "Simple price?"

price.rb

  def simple?
    true unless advanced?
  end

  def advanced?
    specific_days? || period_id? || description? || begin_time? || end_time?
  end

Thanks for any feedback!

1

1 Answers

0
votes

If you rename simple? to is_simple you can implement a virtual attribute in your model

def is_simple
  true unless advanced?
end

def is_simple= _ignored
  # NOOP
end

def advanced?
  specific_days? || period_id? || description? || begin_time? || end_time?
end

and then use :is_simple in your form

= f.input :is_simple, as: :radio, label: "Simple price?"

The renaming is necessary because def simple?= will lead to a syntax-error.