2017-08-10 96 views
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} 
%> 

回答

1

選擇標記將使用第一個參數爲關鍵。因此,值位於:

params[:user_ids] 

,或者如果您正在使用表單生成

<%= form_for @project do |f| %> 
<%= f.select :user_ids, 
    options_from_collection_for_select(User.all, "id", "name", @project.user), 
    { :multiple => true, :size =>10} 
%> 
<% end %> 

那麼將在project_params

+0

啊,這是愚蠢的我......用錯了params:/感謝您的幫助! –

相關問題