0

I have a drop down menu from front end whose value are"true" and "false" as a string respectively.

  {label: 'No', value: 'false'},
  {label: 'Yes', value: 'true'}

They will be insert into a column type with Boolean type.

As you can see, it may be problematic because the type mismatch.

The error i got in the console:

POST http://localhost:3000/api/my_forms.json 422 (Unprocessable Entity)

So is there a efficient way so that once the data is received,it will l be converted into Boolean

I am using Postgresql for my DB and React for my front-end, if that helps.

4
  • it may be problematic... But is it in fact problematic? Commented Mar 6, 2018 at 16:24
  • it is, I am encountering an error while trying to save a form and i ran into an :unprocessable_entity error Commented Mar 6, 2018 at 16:26
  • Interesting. It would likely be helpful, then, to include the error trace, etc. with your question. BTW, you might find this Q&A helpful. Commented Mar 6, 2018 at 16:32
  • @jvillian I will try to put a byebug in between so i can get a trace of it. Commented Mar 6, 2018 at 16:50

1 Answer 1

1

if it is effectively passed as a string, you could add a callback in your model to ensure the value is correctly transformed

For example :

class YourModel
  before_validation :format_field,
                    if: proc { |model|
                      model.your_field.is_a?(String)
                    }
  def format_field
    your_field = (your_field == "true")
  end
end

Or a before action on your controller :

before_action :format_problematic_field
def format_problematic_field
  return unless params[:your_field].is_a?(String)
  params[:your_field] = params[:your_field] == "true"
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.