グループ機能を制作しているのですが、グループにユーザーを追加すると他のグループにもユーザーが追加されてしまいます。
初学者なのでわかりやすく教えていただきたいです。
circle.controller class CirclesController < ApplicationController before_action :set_circle, only: [:update, :destroy, :edit] before_action :set_parents, only: [:new, :create, :destroy] before_action :owner_user, only: [:edit, :destroy] def index @circles = Circle.paginate(page: params[:page]).search(params[:search]) end def member @members = CircleUser.paginate(page: params[:page]) end def new @circle = Circle.new end def show @circle = Circle.find(params[:id]) @circleposts = @circle.circleposts.paginate(page: params[:page]) @circleposts = Circlepost.where(circle_id: @circle.id).all end def create @circle = Circle.new(circle_params) @circle.owner = current_user if @circle.save @circle_user = @circle.owner flash[:success] = "作成しました" redirect_to root_url else render :new end end def edit @circle = Circle.find(params[:id]) end def update @circle = Circle.find(params[:id]) if @circle.update(circle_params) flash[:success] = "サークル情報が更新されました" redirect_to @circle else render 'edit' end end def destroy @circle = Circle.find_by(id: params[:id]) return redirect_to :root if @circle.nil? @circle.destroy flash[:success] = 'サークルを削除しました。' redirect_to :root end def search if params[:name].present? @circles = Circle.where('name LIKE ?', "%#{params[:name]}%") else @circles = Circle.none end end def set_parents @parents = Category.where(ancestry: nil) end def get_category_children @category_children = Category.find("#{params[:parent_id]}").children end def get_category_grandchildren @category_grandchildren = Category.find("#{params[:child_id]}").children end def top respond_to do |format| format.html format.json do if params[:parent_id] @childrens = Category.find(params[:parent_id]).children elsif params[:children_id] @grandChilds = Category.find(params[:children_id]).children elsif params[:gcchildren_id] @parents = Category.where(id: params[:gcchildren_id]) end end end end private def owner_user redirect_to(root_url) unless current_user && @circle.owner? end def circle_params params.require(:circle).permit(:categries_id, :category_grandchildren, :category_children, :parent_id, :name, :place, :time, :homepage, :profile, { :user_ids => [] }) end def set_circle @circle = Circle.find(params[:id]) end end
circle_user.controller class CircleUsersController < ApplicationController def index @circles = current_user.circles.paginate(page: params[:page]) end def create @circle_user = CircleUser.create(circle_id: circle_user_params[:circle_id], user_id: circle_user_params[:user_id]) Apply.find(circle_user_params[:apply_id]).destroy! redirect_to circle_applies_url(@circle_user.circle) # @circle.user = current_user flash[:success] = "「#{@circle_user.user.name}」が、サークル「#{@circle_user.circle.name}」へ加入しました。" end def destroy @circle_user = CircleUser.find(params[:id]) @circle.user.destroy! @circle = Circle.find(params[:circle_id]) redirect_to circle_url(@circle), notice: "サークル「#{@circle.name}」を退会しました。" end def show @circle_user = CircleUser.find(params[:id]) end private def circle_user_params params.permit(:circle_id, :user_id, :apply_id) end end
あなたの回答
tips
プレビュー