Так что я создаю приложение для отправки. Использование Rails 4 Ruby 2Добавление нескольких User_id в форму вызова Rails 4 Ruby 2
Я бы хотел, чтобы пользователь заполнил форму для звонка, чтобы добавить 4 других пользователя к вызываемому ими вызову. (form ect. Будет отображаться ниже). Я создал таблицу соединений, и модели пользователей и вызовов были обновлены ассоциацией has_and_belongs_to_many. Здесь я действительно чувствую себя слабым. Я не уверен, как добавить каждого дополнительного пользователя в форму, поскольку модель вызова ссылается только на один user_id, так как форма стоит сейчас, даже если я выбираю 4 отдельных пользователя, он отображает только одного пользователя во всех 4 ящиках пользователя. Как я могу сделать так, чтобы каждый дополнительный модуль, добавленный к вызову либо из обновления, либо из созданного, будет добавлен? а затем как передать это в views.html.erb и show.html.erb?
Моя форма выглядит следующим образом:
<%= @form_for(@call) do |f| %>
<div class="panel panel-success" id="responding-box">
<div class="panel-heading"><center><h4>Units Responding</h4></center></div>
<table class="table">
<thead>
<tr>
<th><center>Unit #1</center></th>
<th><center>Time On Scene</center></th>
<th><center>Time Clear</center></th>
</tr>
</thead>
<tbody>
<tr>
<td><center><%= f.collection_select(:user_id, User.all, :id, :employee_ident, {}, { :multiple => false }) %></center></td>
<td><center><%= f.time_select :unit_on_scene %></center></td>
<td><center><%= f.time_select :unit_clear %></center></td>
</tr>
</tbody>
</table>
<br>
<table class="table">
<thead>
<tr>
<th><center>Unit #2</center></th>
<th><center>Time On Scene</center></th>
<th><center>Time Clear</center></th>
</tr>
</thead>
<tbody>
<tr>
<td><center><%= f.collection_select(:user_id, User.all, :id, :employee_ident, {}, { :multiple => false }) %></center></td>
<td><center><%= f.time_select :unit2_os %></center></td>
<td><center><%= f.time_select :unit2_cl %></center></td>
</tr>
</tbody>
</table>
<br>
<table class="table">
<thead>
<tr>
<th><center>Unit #3</center></th>
<th><center>Time On Scene</center></th>
<th><center>Time Clear</center></th>
</tr>
</thead>
<tbody>
<tr>
<td><center><%= f.collection_select(:user_id, User.all, :id, :employee_ident, {}, { :multiple => false }) %></center></td>
<td><center><%= f.time_select :unit3_os %></center></td>
<td><center><%= f.time_select :unit3_cl %></center></td>
</tr>
</tbody>
</table>
<br>
<table class="table">
<thead>
<tr>
<th><center>Unit #4</center></th>
<th><center>Time On Scene</center></th>
<th><center>Time Clear</center></th>
</tr>
</thead>
<tbody>
<tr>
<td><center><%= f.collection_select(:user_id, User.all, :id, :employee_ident, {}, { :multiple => false }) %></center></td>
<td><center><%= f.time_select :unit4_os %></center></td>
<td><center><%= f.time_select :unit4_cl %></center></td>
</tr>
</tbody>
</table>
</div>
</div>
Моя Join Table выглядит следующим образом:
create_table "calls_users", id: false, force: :cascade do |t|
t.integer "call_id"
t.integer "user_id"
end
add_index "calls_users", ["call_id"], name: "index_calls_users_on_call_id", using: :btree
add_index "calls_users", ["user_id"], name: "index_calls_users_on_user_id", using: :btree
Мои Call.rb Модель выглядит следующим образом:
class Call < ActiveRecord::Base
has_and_belongs_to_many :users
end
Мой пользователя .rb Модель выглядит так:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :lockable, :timeoutable
has_and_belongs_to_many :calls
end
и мой Calls контроллер выглядит следующим образом:
class CallsController < ApplicationController
before_action :set_call, only: [:show, :edit, :update, :destroy]
# GET /calls
# GET /calls.json
def index
@calls = Call.all
@active_calls = @calls.select{|x| x.status == 'ACTIVE'}
@pending_calls = @calls.select{|x| x.status == 'PENDING'}
end
# GET /calls/1
# GET /calls/1.json
def show
end
# GET /calls/new
def new
@call = Call.new
end
# GET /calls/1/edit
def edit
@call = Call.find(params[:id])
end
# POST /calls
# POST /calls.json
def create
@call = Call.new(call_params)
respond_to do |format|
if @call.save
format.html { redirect_to @call, notice: 'Call was successfully created.' }
format.json { render :show, status: :created, location: @call }
else
format.html { render :new }
format.json { render json: @call.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /calls/1
# PATCH/PUT /calls/1.json
def update
respond_to do |format|
if @call.update(call_params)
format.html { redirect_to @call, notice: 'Call was successfully updated.' }
format.json { render :show, status: :ok, location: @call }
else
format.html { render :edit }
format.json { render json: @call.errors, status: :unprocessable_entity }
end
end
end
# DELETE /calls/1
# DELETE /calls/1.json
def destroy
@call.destroy
respond_to do |format|
format.html { redirect_to calls_url, notice: 'Call was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_call
@call = Call.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def call_params
params.require(:call).permit(:call_time, :status, :primary_type, :secondary_type, :site, :address, :unit_1, :unit_2, :unit_3, :unit_4, :call_details, :unit_on_scene, :unit_clear, :call_num, :site_id, :user_id, :unit2_os, :unit2_cl, :unit3_os, :unit3_cl, :unit4_os, :unit4_cl)
end
end
Я измученный на это и не может продолжаться, пока я не получу эту разобраться! Пожалуйста, любая помощь будет принята с благодарностью!
Это отличная маленькая жемчужина! спасибо, что указали! –
Добро пожаловать! Счастливое кодирование! –