Ich bin derzeit ein collection_select von Ranches meine Mitarbeiter hinzuzufügen versuchen, und ich sah, dass es besser ist, eine zusätzliche Tabelle zu erstellen, um diese Vereinigung zu machen. Und ich einige Tutorial folgen, aber nicht funktioniert auf meiner Seitecollection_select schafft nicht die Zuordnungstabelle
Dies ist mein Code:
Staffs/_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 %>
Meine Modelle: - Ranch:
has_many :ranchstaffs
has_many :staffs, :through => :ranchstaffs
- Mitarbeiter:
has_many :ranchstaffs
has_many :ranches, :through => :ranchstaffs
-Ranchstaff:
belongs_to :ranch
belongs_to :staff
Mitarbeiter Controller:
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
Können Sie mir erklären, warum das Modell ranchstaff nicht nach einer Schaffung eines neuen Mitarbeiter geschaffen wurde?
Wo ist Ihre 'create' Methode ?. Hier instantiierst du es einfach. – Abhinay
Hallo @Abhinay, danke für deine Antwort, ich füge einfach die create-Funktion hinzu. Was meinst du mit nur analysieren? – tutoto
Wenn Sie mit Schienen 4+ dann wird die ranch_id erlaubt und in Ihrem 'Staff' Modell sind Sie die verschachtelten Attribute zu akzeptieren? – Deep