simple form multiple forms for same resource type on page

◇◆丶佛笑我妖孽 提交于 2021-02-11 07:29:57

问题


I'm using simple_form to create forms for same resource types, it works fine but I've one problem with the javascript due to the fact that all inputs have the same id.

For example, if my model is car and my field is brand, all the inputs for this field on al lteh forms have the same id car_brand.

Is there a way to add a prefix to the id generated by simple_form inputs Without adding id to each input


回答1:


Pass your each form declaration a namespace attribute like below to create uniqness.

<%= simple_form_for @user, namespace: 'first_user_form' do |f| %>

and

<%= simple_form_for @user, namespace: 'second_user_form' do |f| %>

'first_user_form' and 'second_user_form' can be any good names which explains your form purposes. I use them as an example.



来源:https://stackoverflow.com/questions/38191501/simple-form-multiple-forms-for-same-resource-type-on-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!