0
我想在Ruby on Rails上實現多選擇的多對多關係,但是我無法訪問表示所選用戶Id的字符串數組在user_ids字段中。無法訪問Ruby on Rails控制器中的多選值
,以下是我project_params
點擊更新
"project"=>{"name"=>"Project Name",
"client"=>"Client X", "project_url"=>""},
"user_ids"=>["2", "3", "4", "5"], "commit"=>"Update Project", "id"=>"1"}
後,以下是我ProjectsController
代碼
def update
puts project_params[:user_ids=>[]]
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
def project_params
params.require(:project).permit(:id, :name, :client, :project_url, :user_ids => [])
end
線puts project_params[:user_ids=>[]]
打印一個空行。我也試着puts project_params[:user_ids]
和puts project_params[:user_ids[]]
- 沒有一個工作
以下是我_form.html.erb
代碼
<%= select_tag "user_ids",
options_from_collection_for_select(User.all, "id", "name", @project.user),
{ :multiple => true, :size =>10}
%>
啊,這是愚蠢的我......用錯了params:/感謝您的幫助! –