2012-10-04 43 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