如何使用 Rails、Simple_Form 和 Cocoon 创建部分预填的表单?
How do you create partially pre-filled forms with Rails, Simple_Form, and Cocoon?
我有一个表格可以从用户那里收集简历信息。在其中,我需要收集他们说什么语言以及他们对每种语言的熟练程度。我想要 3 种必需的语言,它们已经出现在表单上,并且可以选择允许用户添加其他语言。 Cocoon 非常适合让他们自己添加语言,但是,我怎样才能在表单中获得 3 个语言实例?
cvs/_form.html.haml
= simple_form_for(@cv, url: user_cvs_path) do |f|
...
%hr
.row
%strong Please indicate known languages and ability level in each:
#languages
= f.simple_fields_for :languages do |language|
= render 'language_fields', f: language
.links
= link_to_add_association 'Add Language', f, :languages, class: 'btn btn-success btn-sm'
...
cvs/_language_fields.html.haml
.row.nested-fields
= f.input :name, wrapper_html: { class: 'col-sm-4' }
= f.input :read, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= f.input :write, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= f.input :speak, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= f.input :listen, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= link_to_remove_association "Remove Language", f
在当前状态下,这部分表单如下所示:
但我想要的是:
根据 Pablo 的回答,我已将其添加到我的控制器中:
def new
@cv = @user.build_cv
@cv.languages.build(name: 'English')
@cv.languages.build(name: 'Cantonese')
@cv.languages.build(name: 'Mandarin')
end
我到此为止了:
现在我只需要弄清楚如何使语言 'Fixed' 无法更改,我需要摆脱 Cocoon 提供的 "Remove Language" 链接。
您必须在控制器中创建三种语言:
def new
@cv = @user.build_cv #or current_user.build_cv
3.times do { @cv.languages.build }
end
要获得 3 种特定语言,请在控制器中使用它:
def new
@cv = @user.build_cv
@cv.languages.build(name: 'English')
@cv.languages.build(name: 'Cantonese')
@cv.languages.build(name: 'Mandarin')
end
然后要在表单中显示这三个,使它们不可编辑和不可移动,将表单更改为:
= f.simple_fields_for :languages do |language|
.row
= language.input :name, disabled: true, wrapper_html: { class: 'col-sm-4' }
= language.input :read, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= language.input :write, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= language.input :speak, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= language.input :listen, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
.links
= link_to_add_association 'Add Language', f, :languages, class: 'btn btn-success btn-sm'
我有一个表格可以从用户那里收集简历信息。在其中,我需要收集他们说什么语言以及他们对每种语言的熟练程度。我想要 3 种必需的语言,它们已经出现在表单上,并且可以选择允许用户添加其他语言。 Cocoon 非常适合让他们自己添加语言,但是,我怎样才能在表单中获得 3 个语言实例?
cvs/_form.html.haml
= simple_form_for(@cv, url: user_cvs_path) do |f|
...
%hr
.row
%strong Please indicate known languages and ability level in each:
#languages
= f.simple_fields_for :languages do |language|
= render 'language_fields', f: language
.links
= link_to_add_association 'Add Language', f, :languages, class: 'btn btn-success btn-sm'
...
cvs/_language_fields.html.haml
.row.nested-fields
= f.input :name, wrapper_html: { class: 'col-sm-4' }
= f.input :read, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= f.input :write, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= f.input :speak, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= f.input :listen, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= link_to_remove_association "Remove Language", f
在当前状态下,这部分表单如下所示:
但我想要的是:
根据 Pablo 的回答,我已将其添加到我的控制器中:
def new
@cv = @user.build_cv
@cv.languages.build(name: 'English')
@cv.languages.build(name: 'Cantonese')
@cv.languages.build(name: 'Mandarin')
end
我到此为止了:
您必须在控制器中创建三种语言:
def new
@cv = @user.build_cv #or current_user.build_cv
3.times do { @cv.languages.build }
end
要获得 3 种特定语言,请在控制器中使用它:
def new
@cv = @user.build_cv
@cv.languages.build(name: 'English')
@cv.languages.build(name: 'Cantonese')
@cv.languages.build(name: 'Mandarin')
end
然后要在表单中显示这三个,使它们不可编辑和不可移动,将表单更改为:
= f.simple_fields_for :languages do |language|
.row
= language.input :name, disabled: true, wrapper_html: { class: 'col-sm-4' }
= language.input :read, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= language.input :write, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= language.input :speak, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
= language.input :listen, collection: @language_levels, wrapper_html: { class: 'col-sm-2' }
.links
= link_to_add_association 'Add Language', f, :languages, class: 'btn btn-success btn-sm'