2012-10-04 42 views
0

我想使用collection_select而不是select。Rails:Collection_select將ID保存爲一個條目

<%= form_for(@technol) do |tech| %> 
    <div class="field"> 
    <%= tech.label :tech %><br /> 
    <%= tech.select(:tech, Technol.all.map {|p| [p.tech]}.uniq,:prompt => "Select a previous role") %> 
    </div> 
<%end%> 

此代碼的工作原理允許我將一種技術鏈接到項目。我試圖做到這一點:

<%= form_for(@technol) do |tech| %> 
    <div class="field"> 
    <%= tech.label :tech %><br /> 
    <%= tech.collection_select(:tech, Technol.all, :id, :tech, {}, {:multiple => true}) %> 
    </div> 
<%end%> 

的collection_select出現,所有的技術都在下拉列表中顯示,當我挑幾個,並提交項目,這些技術會顯示爲一個條目,其ID 。

--- - '' - '11' - '12' - '13'

這裏是我創造的行動,我認爲是造成問題:

def create 
    @project = Project.new(params[:project]) 
    @technol = Technol.new(params[:tech]) 

    params[:technol].each_value do |tech| 

    technology = Technol.find_or_create_by_tech(tech) 

    @project_technol = @project.projecttechnols.build(:technol_id => technology.id) 
end 

    respond_to do |format| 
     if @project.save 
     format.html { redirect_to @project, notice: 'Project was successfully created.' } 
     format.json { render json: @project, status: :created, location: @project } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @project.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

,我認爲它的一些問題,我是循環的方式。希望有人能看到它。我是新來的鐵路,所以請記住這一點,當試圖幫助我。提前致謝。

UPDATE

我曾試圖改變collection_select到:

<%= tech.select(:technol, :id, Technol.all.map {|p| [p.tech]}.uniq,:prompt => "Select a previous role") %> 

,我得到這個錯誤。 wrong number of arguments (7 for 6)

更新羅斯:

wrong number of arguments (7 for 6) 
Extracted source (around line #273): 

273: tech.collection_select(:tech, :tech_ids, Technol.all, :id, :tech, {:prompt => "Select a previous role"}, {:multiple => true}) 
+0

我認爲它集合的語法選擇 嘗試 tech.collection_select(:高科技,:tech_ids,Technol.all,: ID,:技術,{},{:multiple => true}) – Ross

+0

您好,我得到同樣的錯誤,因爲我得到了與我的問題更新。 – Jazz

+0

使用正確的括號,嘗試複製粘貼與我上面評論相同 – Ross

回答

1

使用

collection_select(:test, :tech_ids, Technol.all, :id, :tech, {:prompt => "Select a previous role"}, {:multiple => true}) 
+0

我剛更新我的問題,告訴你當我運行你的代碼時會發生什麼。歡呼聲 – Jazz

+0

ohh沒有看到表單助手,請嘗試上面的編輯 – Ross

+0

我得到一個No方法錯誤。 '未定義的方法「tech_ids」爲#<技術ID:無,技術:無,created_at:無,updated_at:無>。所以我把它改爲':id',我沒有將技術添加到項目中 ' – Jazz