collection_select没有创建关联表

时间:2016-12-08 04:29:03

标签: ruby-on-rails ruby

我目前正在尝试向我的员工添加一系列牧场 我发现创建一个额外的表来建立这种关联会更好。 我遵循一些教程,但不在我身边

这是我的代码:

员工/ _form:

<%= form_for(@staff) do |f| %>


  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>

    <%= fields_for(@staff_ranch) do |x| %>
    <div class="field">

      <%= x.collection_select(:ranch_id, @all_ranch, :id, :name, { }, {:multiple => true}) %>
    </div>
  <%end%>


  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我的模特: - 牧场:

has_many :ranchstaffs
has_many :staffs, :through => :ranchstaffs

- 工作人员:

has_many :ranchstaffs
has_many :ranches, :through => :ranchstaffs

-Ranchstaff:

belongs_to :ranch
belongs_to :staff

员工控制员:

   class StaffsController < ApplicationController
  before_action :set_staff, only: [:show, :edit, :update, :destroy]

  # GET /ranches
  # GET /ranches.json
  def index
    @staffs = current_user.staffs
  end

  # GET /ranches/1
  # GET /ranches/1.json
  def show

  end

  # GET /ranches/new
  def new
    @staff = Staff.new
    @all_ranch = current_user.ranches
    @staff_ranch = @staff.ranchstaffs.build
  end

  # GET /ranches/1/edit
  def edit
  end

  # POST /ranches
  # POST /ranches.json
  def create
    @staff = Staff.new(staff_params)

    @staff.update(user_id: current_user.id)
    respond_to do |format|
      if @staff.save
        format.html { redirect_to @staff, notice: 'Staff was successfully created.' }
        format.json { render :show, status: :created, location: @staff }
      else
        format.html { render :new }
        format.json { render json: @staff.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /ranches/1
  # PATCH/PUT /ranches/1.json
  def update
    respond_to do |format|
      if @staff.update(staff_params)
        format.html { redirect_to @staff, notice: 'Staff was successfully updated.' }
        format.json { render :show, status: :ok, location: @staff }
      else
        format.html { render :edit }
        format.json { render json: @staff.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /ranches/1
  # DELETE /ranches/1.json
  def destroy
    @staff.destroy
    respond_to do |format|
      format.html { redirect_to staffs_url, notice: 'Ranch was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_staff
      @staff = Staff.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def staff_params
      params.require(:staff).permit(:name, :user_id, :cat, :ranch_id)
    end
end

你能解释一下为什么在新员工创建后没有创建模型ranchstaff吗?

2 个答案:

答案 0 :(得分:1)

当您使用fields_for时,您使用的是嵌套表单,但您不允许正确使用这些参数。首先在表单中进行更改:

<%= f.fields_for(@staff_ranch) do |x| %>
  <div class="field">
    <%= x.collection_select(:ranch_id, @all_ranch, :id, :name, { }, {:multiple => true}) %>
  </div>
<% end %>

然后在你的控制器中:

def staff_params
  params.require(:staff).permit(:name, :user_id, :cat, ranchstaff_attributes: [ranch_id: []])
end

在您的Staff模型中写道:

accepts_nested_attributes_for :ranchstaffs

然后,在创建ranchstaff时,应创建User

答案 1 :(得分:0)

你的ranch_id正在排成一列。所以你必须指定ranch_id是强参数中的数组。 所以你的staff_params方法看起来像这样

def staff_params
  params.require(:staff).permit(:name, :user_id, :cat, :staff_ranch_attributes =>[:ranch_id => []])
end