Search code examples
ruby-on-railsurlhandlebars.jsruby-on-rails-5simple-form

Rails & Handlebars: bad URI error for template URL


In an older project I used this Handlebars template:

script id='user-form-template' type='text/x-handlebars-template'
  = simple_form_for User.new, method: 'post', url: '/users/{{id}}' do |f|
    = hidden_field_tag :_method, '{{method}}'
    = f.input :name
    = f.input :email
    ...

This created a form within a Handlebars template with this action:

action="/users/{{id}}"

I used it to create new (leave id blank, set _method to POST) and edit existing (set id, set _method to PATCH) users.

In my new project (Rails 5.2.1) I get a URI::InvalidURIError message:

bad URI(is not URI?): /users/{{id}}

Is there a better way to build a template with a form? I would like to keep SimpleForm to automatically create the bootstrap form HTML.


Solution

  • I changed the template to

    = simple_form_for User.new, method: 'post', data: { url: '/users/{{id}}' }
    

    I then change the data-url back to url after replacing the placeholders in the template:

    $('form').attr('action', $('form').data('url'));
    

    Not the best solution, but I can work with it.