Question

I want to edit a has_many through relation, but instead of editing the relation, it creates a new model.

In my form:

<%= form_for @service do |f| %>
   <%= f.fields_for :service_users do |ac| %>
   <% end %>
<% end %>

In my model:

 class Service < ActiveRecord::Base
   has_many :service_users
   has_many :users, :through => :service_users

   accepts_nested_attributes_for :service_users
 end

Begin situation: Begin situation When i update the comments field: When i update the comments field

After updating i see the edited relation as a duplication of the first one. In some way i have to check if there're already relations present, but how?

Update:

My controller:

    class ServicesController < ApplicationController
  before_action :set_service, only: [:show, :edit, :update, :destroy, :users]

  before_filter :authenticate_user!

  # GET /services
  # GET /services.json
  def index
    services = current_user.available_services
    @available_services = services.group_by { |t| t.date.beginning_of_month }
  end

  # GET /services/1
  # GET /services/1.json
  def show
    # service_users = current_user.service_users
    # 
    # Service.find_each do |service|
    #   unless service_users.detect { |m| m.service_id == service.id }
    #     current_user.service_users.build service_id: service.id
    #   end
    # end
    # 
    @available_users = @service.available_users.group_by { |u| u.group }
    @planned_users = @service.planned_users.group_by { |u| u.group }
    @reserve_users = @service.reserve_users.group_by { |u| u.group }
  end

  # GET /services/new
  def new
    @service = Service.new
  end

  # GET /services/1/edit
  def edit
    @service.service_users.create
  end

  # POST /services
  # POST /services.json
  def create
    @service = Service.new(service_params)
    p service_params
    respond_to do |format|
      if @service.save
        format.html { redirect_to @service, notice: 'Service was successfully created.' }
        format.json { render action: 'show', status: :created, location: @service }
      else
        format.html { render action: 'new' }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /services/1
  # PATCH/PUT /services/1.json
  def update
    respond_to do |format|
      p service_params[:service_users_attributes]
      if @service.update(service_params)
        format.html { redirect_to @service, notice: 'Service was successfully updated.' }
        format.json { render action: 'show', status: :ok, location: @service }
      else
        format.html { render action: 'edit' }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end

  def users
    @users = @service.users
  end

  # DELETE /services/1
  # DELETE /services/1.json
  def destroy
    @service.destroy
    respond_to do |format|
      format.html { redirect_to services_url }
      format.json { head :no_content }
    end
  end

  def destroy_association
    if params[:id].present?
      ServiceUser.find(params[:id]).delete
      redirect_to root_path
    end
  end

  def make_user_available_for_service
    p '########'
    p params
    p @service
    redirect_to root_path
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def service_params
      params.require(:service).permit(:date, :comments,
                                      service_users_attributes: [:user_id, 
                                                                :service_id,
                                                                :availability, 
                                                                :comments],
                                      service_groups_attributes: [:service_id, 
                                                                :group_id, 
                                                                :start_time, 
                                                                :end_time])
    end

end
Était-ce utile?

La solution

Try build instead of create.

Like this:

def edit
 @service.service_users.build
end

And check your service_params. Your missing an id for the service_users.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top