Ruby on rails 铁路及;把手:模板URL的错误URI错误
在一个较旧的项目中,我使用了此把手模板:Ruby on rails 铁路及;把手:模板URL的错误URI错误,ruby-on-rails,url,handlebars.js,ruby-on-rails-5,simple-form,Ruby On Rails,Url,Handlebars.js,Ruby On Rails 5,Simple Form,在一个较旧的项目中,我使用了此把手模板: 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
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
...
$('form').attr('action', $('form').data('url'));
这将使用以下操作在把手模板中创建表单:
action="/users/{{id}}"
我使用它来创建新用户(将id留空,将_方法设置为POST)和编辑现有用户(将id设置为,将_方法设置为PATCH)
在我的新项目(Rails 5.2.1)中,我得到一条URI::InvalidURIError消息:
bad URI(is not URI?): /users/{{id}}
有没有更好的方法用表单构建模板?我想保留SimpleForm以自动创建引导表单HTML。因为您正在为用户执行
simple\u form\u。new
,而不是@existing\u User的simple\u form\u,我打赌您需要删除URL的/{{id}
/users
我将模板更改为
= simple_form_for User.new, method: 'post', data: { url: '/users/{{id}}' }
然后,在替换模板中的占位符后,我将数据url
更改回url
:
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
...
$('form').attr('action', $('form').data('url'));
不是最好的解决方案,但我可以使用它。在我以前的项目中,我可以为新用户和现有用户使用该表单,效果非常好。没有{{id},我只能对新用户使用它,因为我不能用手柄添加它。SimpleForm使用User.new获取模型信息,URL并不重要。在这种情况下,括号是唯一的问题。