Search code examples
ruby-on-railsvalidationattributesform-helpersparsley

Form Tag Helpers Cannot Process '-' in Rails 4


I am trying to use Parsley for Rails 4 with Form Tag Helpers.

As said in the documentation

"Options" provide a way for custom attributes; However, Parsley uses the notation "data-parsley". Rails cannot interpret the "-" and application brings out an error.

Is there a workaround?

Thanks,


Solution

  • There are 2 ways to write data tags in Rails (or other tags with -):

    data: {parsley: 'something'} # -> data-parsley="something"
    

    or

    'data-parsley' => 'something' # -> data-parsley="something"
    

    Also, there is a strange, but useful behavior: inside data braces, you can use _ and it will renders as -, for ex:

    data: {customer_id: 'id'} # -> data-customer-id="id"
    

    The same, as

    data: {customer: {id: 'id'}} # -> data-customer-id="id"