2016-12-08 15 views
1

現在、私のスタッフに牧場のcollection_selectを追加しようとしています そして、この関連付けを行うために余分なテーブルを作成する方が良いことがわかりました。牧場 - :collection_selectが関連付けテーブルを作成していません

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

スタッフ/ _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 %> 

私のモデル と私はいくつかのチュートリアルに従いますが、私の側に動作しない

これは私のコードです

- スタッフ:

-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は、新しいスタッフの作成後に作成されなかった理由をあなたは私を説明できますか?

+0

ようになるでしょう?。ここであなたはただそれをインスタンス化しています。 – Abhinay

+0

こんにちは@Abhinay、あなたの応答のおかげで、私はちょうど作成機能を追加します。ちょうど分析することは何を意味するのですか? – tutoto

+0

4+を使用している場合、ranch_idは許可されており、 'Staff'モデルではネストされた属性を受け入れていますか? – Deep

答えて

1

fields_forを使用しているため、ネストされたフォームを使用していますが、パラメータを適切に許可していません。ときUserあなたranchstaffを作成する必要があります次に

accepts_nested_attributes_for :ranchstaffs 

:あなたのコントローラで

<%= 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モデル書き込みの最初のフォームで変更を行いますが作成されています。

+0

すごくおかげさまでしたが、新しいスタッフを作成するときに実際に作成したいのですが、新しいスタッフを実際のユーザーの牧場に結びつけることです。私はそれをどのように生成できるか知っていますか? – tutoto

+0

これは同じことをするはずです。スタッフを作成する際には、牧場を選択してスタッフに関連付けられます。 – Deep

0

あなたのranch_idは配列に入っています。ですから、ranch_idが強いパラメータで配列になるように指定する必要があります。 ので、あなたのstaff_params方法は、あなたの `create`方法がある。この

def staff_params 
    params.require(:staff).permit(:name, :user_id, :cat, :staff_ranch_attributes =>[:ranch_id => []]) 
end 
関連する問題