Rails: selectize multiselect 只提交最后一次选择
Rails: Selectize multiselect only submits the last selection
我有一个用于创建新用户的表单。我正在尝试使用 gem selectize-rails
来允许添加多个相关用户的选项:
views/users/new.html.erb
<%= form_for @user, :url => users_path do |f| %>
<select name="users" id='select-user'>
<option value="">Select at least one user</option>
<% User.all.each do |user| %>
<option value="<%= user.name %>"><%= user.name %></option>
<% end %>
</select>
<script type="text/javascript">
var select = $('#select-user').selectize({
persist: false,
maxItems: 3,
create: false,
valueField: 'name',
labelField: 'name',
searchField: ['name'],
render: {
item: function(item, escape) {
return '<div>' +
(item.name ? '<span class="select-name">' + escape(item.name) + ',</span>' : '') +
'</div>';
},
option: function(item, escape) {
var label = item.name;
return '<div>' +
'<span class="select-label">' + escape(label) + '</span>' +
'</div>';
}
},
preload: false,
load: function(query, callback) {}
});
</script>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
controllers/user_controller.rb
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_path
else
redirect_to new_user_path
end
end
private
def user_params
params.require(:user).permit(:name, :users)
end
在 new.html.erb
上,代码运行良好:我最多可以添加三个用户。但是当我提交它时,params["users"]
只包含我添加的最后一个用户,无论我尝试添加多少。有谁知道为什么会这样?
变化:
<select name="users" id='select-user'>
到
<select name="users" id='select-user' multiple='multiple'>
这应该使 rails 期望从 select 获得多个值。
整个 select 部分,可以使用 rails select 助手重写。
<select name="users" id='select-user'>
<option value="">Select at least one user</option>
<% User.all.each do |user| %>
<option value="<%= user.name %>"><%= user.name %></option>
<% end %>
</select>
变成
<%= select :user, options_for_collection_for_select(User.all, :name, :name),
{ prompt: "Please select.." },
{ multiple: true } %>
有关详细信息,请参阅 http://guides.rubyonrails.org/v2.3.11/form_helpers.html#making-select-boxes-with-ease and http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html。
我有一个用于创建新用户的表单。我正在尝试使用 gem selectize-rails
来允许添加多个相关用户的选项:
views/users/new.html.erb
<%= form_for @user, :url => users_path do |f| %>
<select name="users" id='select-user'>
<option value="">Select at least one user</option>
<% User.all.each do |user| %>
<option value="<%= user.name %>"><%= user.name %></option>
<% end %>
</select>
<script type="text/javascript">
var select = $('#select-user').selectize({
persist: false,
maxItems: 3,
create: false,
valueField: 'name',
labelField: 'name',
searchField: ['name'],
render: {
item: function(item, escape) {
return '<div>' +
(item.name ? '<span class="select-name">' + escape(item.name) + ',</span>' : '') +
'</div>';
},
option: function(item, escape) {
var label = item.name;
return '<div>' +
'<span class="select-label">' + escape(label) + '</span>' +
'</div>';
}
},
preload: false,
load: function(query, callback) {}
});
</script>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
controllers/user_controller.rb
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to root_path
else
redirect_to new_user_path
end
end
private
def user_params
params.require(:user).permit(:name, :users)
end
在 new.html.erb
上,代码运行良好:我最多可以添加三个用户。但是当我提交它时,params["users"]
只包含我添加的最后一个用户,无论我尝试添加多少。有谁知道为什么会这样?
变化:
<select name="users" id='select-user'>
到
<select name="users" id='select-user' multiple='multiple'>
这应该使 rails 期望从 select 获得多个值。
整个 select 部分,可以使用 rails select 助手重写。
<select name="users" id='select-user'>
<option value="">Select at least one user</option>
<% User.all.each do |user| %>
<option value="<%= user.name %>"><%= user.name %></option>
<% end %>
</select>
变成
<%= select :user, options_for_collection_for_select(User.all, :name, :name),
{ prompt: "Please select.." },
{ multiple: true } %>
有关详细信息,请参阅 http://guides.rubyonrails.org/v2.3.11/form_helpers.html#making-select-boxes-with-ease and http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html。