【问题标题】:Adding Several User_id's into a Call Form Rails 4 Ruby 2将几个 User_id 添加到调用表单 Rails 4 Ruby 2
【发布时间】:2015-11-13 09:39:01
【问题描述】:

所以我正在构建一个调度应用程序。使用 Rails 4 Ruby 2

我希望用户在填写呼叫表单时能够将最多 4 个其他用户添加到他们正在创建的呼叫中。 (表格等。将在下面显示)我创建了一个联接表,并且用户和呼叫模型都已更新为 has_and_belongs_to_many 关联。这是我变得非常虚弱的地方。我不确定如何将每个额外的用户添加到表单中,因为调用模型仅引用一个 user_id,因此表单现在存在,即使我选择 4 个单独的用户,它也只会在所有 4 个用户框中显示一个用户。我怎样才能使从更新或从创建中添加到调用中的每个附加单元都将被添加?然后如何将其传输到 index.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>

我的联接表如下所示:

 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

我的 User.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

我的呼叫控制器看起来像:

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

我对此很着迷,在我解决这个问题之前无法继续!请任何帮助将不胜感激!

【问题讨论】:

    标签: html ruby postgresql ruby-on-rails-4


    【解决方案1】:

    您应该看看cocoon gem。这将帮助你完成你想要的!

    【讨论】:

    • 那是一块很棒的小宝石!感谢您指出!
    猜你喜欢
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多