How to validate a binary field using Ruby on Rails?

yosei

I want to ensure that a binary field has always value. I added a validation code like below.

class Foo < ActiveRecord::Base
  validates :b, presence: true
end

However, the change seems to cause the error.

$ rails c
> Foo.create(b:File.read('b.jpg'))

ArgumentError: invalid byte sequence in UTF-8

The error doesn't always appear. Only when the binary data has non-ascii codes.

How can I validate the binary field?

I made the environment like below. A image file(b.jpg, less than 16KB) is also needed.

$ rails --version
Rails 4.2.0
$ rails new test_binary --database=mysql
$ cd test_binary/
$ rails g model foo b:binary
$ rake db:create db:migrate
mu is too short

File.read returns a String that will claim to have UTF-8 encoding by default. That means that this:

Foo.create(b: File.read('b.jpg'))

is really:

some_utf8_string = File.read('b.jpg')
Foo.create(b: some_utf8_string)

But a JPEG will rarely be a valid UTF-8 string so you're going to get that ArgumentError whenever someone tries to treat it as UTF-8.

You can specify an encoding when you read your JPEG:

Foo.create(b: File.read('b.jpeg', encoding: 'binary'))

That should get past your encoding problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to validate presence of at least one field in form in Ruby on Rails

From Dev

validate the date of an 'event' on ruby on rails using activerecord

From Dev

How to validate using regex to fit the empty line (no digit) with constraint for Ruby on Rails

From Dev

how to validate array input field using javascript

From Dev

How to validate input field using php

From Dev

How to validate the form field using input append

From Dev

how to validate mobile and also allow blank field in mobile in ruby

From Dev

How to validate a table in Rails using another table

From Dev

how to test validate presence unless another field is true rails?

From Dev

Rails - How to validate radio button and text field values?

From Dev

how to test validate presence unless another field is true rails?

From Dev

Ruby/Rails - How can you validate against decimal scale?

From Dev

Ruby on Rails: How to validate an object being added to an array?

From Dev

Cannot create a model with 'binary' field using Active Admin and Rails 4.1

From Dev

Cannot create a model with 'binary' field using Active Admin and Rails 4.1

From Dev

Validate unique field in Rails app

From Dev

Rails Validate Field is True, If Present

From Dev

Validate correctness of a regular expression - Ruby/Ruby on Rails

From Dev

Sum up the total of a field in ActiveRecord using inject in Ruby/Rails

From Dev

How to validate the form using angularjs when the name field has .(dot) in it?

From Dev

How would i validate a text field in html using javascript?

From Dev

How to validate a field to be unique for some rows using Laravel?

From Dev

How to validate the form using angularjs when the name field has .(dot) in it?

From Dev

how to validate form field in cakephp using model and controller

From Dev

How to validate a url in Rails

From Dev

How to validate a boolean in Rails

From Dev

Ruby on Rails: how to set size of number_field

From Dev

Ruby on Rails: How to customise error message for MailForm when field is blank

From Dev

Rails/Ruby - How to check if an attr_accessor field exists?

Related Related

  1. 1

    How to validate presence of at least one field in form in Ruby on Rails

  2. 2

    validate the date of an 'event' on ruby on rails using activerecord

  3. 3

    How to validate using regex to fit the empty line (no digit) with constraint for Ruby on Rails

  4. 4

    how to validate array input field using javascript

  5. 5

    How to validate input field using php

  6. 6

    How to validate the form field using input append

  7. 7

    how to validate mobile and also allow blank field in mobile in ruby

  8. 8

    How to validate a table in Rails using another table

  9. 9

    how to test validate presence unless another field is true rails?

  10. 10

    Rails - How to validate radio button and text field values?

  11. 11

    how to test validate presence unless another field is true rails?

  12. 12

    Ruby/Rails - How can you validate against decimal scale?

  13. 13

    Ruby on Rails: How to validate an object being added to an array?

  14. 14

    Cannot create a model with 'binary' field using Active Admin and Rails 4.1

  15. 15

    Cannot create a model with 'binary' field using Active Admin and Rails 4.1

  16. 16

    Validate unique field in Rails app

  17. 17

    Rails Validate Field is True, If Present

  18. 18

    Validate correctness of a regular expression - Ruby/Ruby on Rails

  19. 19

    Sum up the total of a field in ActiveRecord using inject in Ruby/Rails

  20. 20

    How to validate the form using angularjs when the name field has .(dot) in it?

  21. 21

    How would i validate a text field in html using javascript?

  22. 22

    How to validate a field to be unique for some rows using Laravel?

  23. 23

    How to validate the form using angularjs when the name field has .(dot) in it?

  24. 24

    how to validate form field in cakephp using model and controller

  25. 25

    How to validate a url in Rails

  26. 26

    How to validate a boolean in Rails

  27. 27

    Ruby on Rails: how to set size of number_field

  28. 28

    Ruby on Rails: How to customise error message for MailForm when field is blank

  29. 29

    Rails/Ruby - How to check if an attr_accessor field exists?

HotTag

Archive