私はユーザーモデル、グループモデル(Cliq)、およびグループメンバーモデル(Cliq_Membership)を持っています。これまでのところすべてがうまくいっているようです。私は現在、ユーザーがグループを作成して「所有」し、「所有者」がグループを離れると(グループメンバーシップを破棄する)、グループ全体が破棄されるようにしています。グループには1人のオーナーと多数のメンバーがいます。私は、ユーザーが「メンバー」であることを要求しなければならないようにしたいと思います。私は "所有者"が要求を見て、要求を受け入れる/拒否する唯一のものにしたい。明確にするためユーザーにグループメンバーシップを要求する方法を教えてください。
:
- 私はCliqsは所有者だけが見ることができるはずメンバー
- であることをユーザーに要求することができるようにしたいユーザーは、グループメンバー
- ことを要求する必要がありますする必要友人の要求を受け入れる/拒否する
- 私は、関係が "双方向/自己参照"であることを望みます。つまり、私は
がどのようにこれを実現するかグループ=ユーザー/メンバーは、グループや他のメンバー
は、これまでの私のコードです:
モデル:
class User < ActiveRecord::Base
has_many :uploads
has_one :owned_cliq, foreign_key: 'owner_id', class_name: 'Cliq', dependent: :destroy
has_many :cliq_memberships
has_many :cliqs, through: :cliq_memberships
end
class CliqMembership < ActiveRecord::Base
belongs_to :cliq
belongs_to :user
end
class Cliq < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :cliq_memberships, dependent: :destroy
has_many :members, through: :cliq_memberships, source: :user
end
コントローラ:
class CliqMembershipsController < ApplicationController
def create
#Cliq or Cliq_ID?
@cliq = Cliq.find(params[:cliq])
@cliq_membership = current_user.cliq_memberships.build(cliq: @cliq)
@cliq.members << current_user
if @cliq_membership.save
flash[:notice] = "Joined #{@cliq.name}"
else
flash[:notice] = "Not able to join Cliq."
end
redirect_to cliq_url
end
def destroy
@cliq_membership = current_user.cliq_memberships.find(params[:id])
@cliq = @cliq_membership.cliq
if @cliq.owner == current_user
@cliq.destroy
flash[:notice] = "Cliq has been deleted."
redirect_to current_user
else
@cliq_membership.destroy
flash[:notice] = "You left the Cliq."
redirect_to current_user
end
end
end
class CliqsController < ApplicationController
def show
@cliq = Cliq.find(params[:id])
end
def new
@cliq = Cliq.new(params[:id])
end
def create
@cliq = current_user.build_owned_cliq(cliq_params)
@cliq.members << current_user
if @cliq.save
redirect_to current_user
else
redirect_to new_cliq_path
end
end
def destroy
#@cliq = current_user.owned_cliq.find(params[:id])
#lash[:alert] = "Are you sure you want to delete your Cliq? Your Cliq and all of its associations will be permanently deleted."
#@cliq.destroy
#if @cliq.destroy
#redirect_to current_user
#flash[:notice] = "You deleted the Cliq."
#else
#redirect_to current_user
#set up error handler
#flash[:notice] = "Failed to delete Cliq."
#end
end
def cliq_params
params.require(:cliq).permit(:name, :cliq_id)
end
end
リクエストを作成(送信)するにはどうすればよいですか? –
これでうまくいくはずです。 –