select の代わりに collection_select を使用しようとしています。
<%= 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%>
このコードは機能し、1 つのテクノロジをプロジェクトにリンクすることができます。私はこれをやろうとしました:
<%= 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 ごとに 1 つのエントリとして表示されます。
--- - '' - '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
ループの仕方に問題があると思います。うまくいけば、誰かがそれを見ることができます。私はレールに慣れていないので、私を助けようとするときはこれを覚えておいてください。前もって感謝します。
アップデート
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} )