ユーザーがプロジェクトを送信できるアプリがあります。フィールドごとに、データベースに新しいデータを入力するか、過去のプロジェクトから古いデータを選択してそのフィールドに入力するかを選択できます。
新しいビューのこのコードでこれを機能させるのに問題があります。
<%= form_for(@technol) do |tech| %>
<%= fields_for(@project_technol) do |ab| %>
<%= text_field_tag :tech, nil, :maxlength => 30 %>
OR<br />
<%= ab.label "All Tech"%> </br>
<%= collection_select( :technols, :id, Technol.all, :id, :tech, {}, {:multiple => true } ) %>
</div>
<% end %>
<% end %>
現時点では、ユーザーはcollection_selectから多くのテクノロジーを選択でき、プロジェクトとともに保存されますが、テキストボックスを使用して独自のテクノロジーを入力するオプションをユーザーに提供しようとしています。
私のコントローラーのアクション:
新着
def new
@project = Project.new
@technol = Technol.new(params[:tech])
@all_technols = Technol.all
tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end
作成
def create
@project = Project.new(params[:project])
params[:technols][:id].each do |tech|
if !tech.empty?
@project.projecttechnols.build(:technol_id => tech)
end
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
要約すると、ユーザーがデータベースに新しい技術を入力し、ドロップダウンから既存の技術を選択して、すべてがプロジェクトとともに保存され、入力された新しい技術が技術テーブルに保存されるようにするオプションが必要です。
何か案は?私はレール初心者なので、答えるときはこれを覚えておいてください。どんな助けでも大歓迎です。前もって感謝します