我试图使用collection_select
创建一个HTML多重选择,以便能够更新一个实体(Student
),它具有另一个实体(SubscriptionList
)的集合作为嵌套属性。这是由HABTM ActiveRecord的关系备份的。
我已经通过脚手架为学生创建了以下表单:
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :file_number %><br>
<%= f.text_field :file_number %>
</div>
<div class="field">
<%= f.label :subscription_list %><br>
<%= f.collection_select(:subscription_lists, SubscriptionList.all, :id, :name, {}, {:multiple => true}) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
,它正确地绘制了多重选择。
但是,如果我填写表单并尝试PUT实体,我将得到以下参数:
{"utf8"=>"✓","_method"=>"patch", "authenticity_token"=>"vXYMRYI1UtX7WJRZM0OPIhHQSSEyNOPyUxkUvScdu45PTL7qVhvlJfQYNvaKG5rw+mvHAAAbf6ViTQ6tE4lV1Q=>", "student"=>{"first_name"=>" Mariana", "last_name"=>"González", "file_number"=>"12345678", "subscription_lists"=>[", "3"]}, "commit"=>"Update student", "controller"=>"students", "action"=>" Update ", "id"=>"14"}
那么,我的Student是
"first_name"=>" Mariana", "last_name"=>"González", "file_number"=>"12345678", "subscription_lists"=>["", "3"]}
我发现接收["", "3"]
作为值是非常奇怪的。为什么我收到这个第一个""
值?
我也在这里张贴我的控制器(动作,update
已被删除为简洁)
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy, :enrollments]
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
puts "nnnnnnnnnThese are the params: #{params.inspect}"
puts "nnnnnThis is student_params object: #{student_params.inspect}nnnand its class #{student_params.class}"
#puts "nnnnnnnnnWill look for SL with ID: #{params[:subscription_lists_id]}"
all_ids = student_params.subscription_lists.collect {|sl| sl.id }
@student.subscription_lists = SubscriptionList.find(all_ids)
#@student.subscription_lists = SubscriptionList.where(id: all_ids)
respond_to do |format|
if @student.update(student_params)
format.html { redirect_to @student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: @student }
else
format.html { render :edit }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
@student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
#params[:student]
#params.require(:foo).permit(:bar, {:baz => [:x, :y]})
#params.require(:student).permit(:first_name, :last_name, :file_number, :subscription_lists)
params.require(:student).permit! # No strong parameters...
end
end
事实上,我宁愿接收Student
与SubscriptionList
的嵌套集合,而不是仅仅接收一个id数组,但我不确定这是否可能。
任何帮助都将非常感激。
最好的祝福
关于您的问题已在collection中得到回答,select总是添加空白值。
你将得到[""]
当你没有选择任何东西或你选择你的选项选择作为默认值。为了避免这种情况,您必须在集合select之前添加hidden_field
。
<div class="field">
<%= f.label :subscription_lists %><br>
<%= f.hidden_field :subscription_lists %>
<%= f.collection_select(:subscription_lists, SubscriptionList.all, :id, :name, {}, {:multiple => true}) %>
</div>
hidden_field
在没有选择时可以帮助您。当你选择它的时候呢?请试试这个
def update
if student_params["subscription_lists"].any?
student_params["subscription_lists"].reject!(&:empty?)
end
respond_to do |format|
if @student.update(student_params)
format.html { redirect_to @student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: @student }
else
format.html { render :edit }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
这就是我最终所做的,主要是遵循本教程。
对于控制器:
class StudentsController < ApplicationController
before_action :set_student, only: [:show, :edit, :update, :destroy, :enrollments]
# PATCH/PUT /students/1
# PATCH/PUT /students/1.json
def update
puts "nnnnnnnnnThese are the params: #{params.inspect}"
puts "nnnnnThis is student_params object: #{student_params.inspect}nnnand its class #{student_params.class}"
@subscription_lists = SubscriptionList.where(:id => params[:subscriptions])
@student.subscription_lists.destroy_all # disassociate the already added
@student.subscription_lists << @subscription_lists
respond_to do |format|
if @student.update(student_params)
format.html { redirect_to @student, notice: 'Student was successfully updated.' }
format.json { render :show, status: :ok, location: @student }
else
format.html { render :edit }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_student
@student = Student.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def student_params
params.require(:student).permit(:first_name, :last_name, :file_number, subscription_lists: [:id])
end
end
和形式:
<div class="field">
<%= f.label :subscription_list %><br>
<%= select_tag "subscriptions", options_from_collection_for_select(SubscriptionList.all, 'id', 'name',@student.subscription_lists.map { |j| j.id }), :multiple => true %>
</div>
希望对大家有用