代码之家  ›  专栏  ›  技术社区  ›  Railsana

Rails&Handlebars:模板URL的错误URI

  •  0
  • Railsana  · 技术社区  · 6 年前

    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
        ...
    

    此操作将在把手模板中创建一个表单:

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

    我用它来创造新的 (保留id为空,将方法设置为POST) 编辑现有 用户。

    在我的新项目(Rails 5.2.1)中,我得到了 URI::无效urierror

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

    有没有更好的方法用表单构建模板?我想让SimpleForm自动创建引导表单HTML。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Jeremy Green    6 年前

    既然你在做 simple_form_for User.new ,而不是 simple_form_for @existing_user /{{id}} URL的一部分。如果你想创建一个新用户 /users .

        2
  •  0
  •   Railsana    6 年前

    我把模板改成

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

    然后我换了 data-url url 替换模板中的占位符后:

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

    不是最好的解决办法,但我能解决。