2017-08-07 19 views
0

這可能非常簡單,但我已經把我的大腦折騰了幾天,但無法弄清楚!我是新手,因此任何指向右側方向將不勝感激!

我有3個型號:集體,group_membership &用戶。我希望用戶能夠通過按集合索引&顯示頁面上的按鈕來加入集體。

我需要一個group_membership控制器嗎?

我想過使用一個表單,它繼承了collective_id & user_id的屬性作爲隱藏字段,連接按鈕作爲表單提交,但我無法弄清楚。如果您需要了解其他信息,請告訴我!
這裏是我所擁有的東西的摘錄。Rails使用3個模型添加到羣組按鈕

模式

class Collective < ActiveRecord::Base 
    has_many :group_memberships, dependent: :destroy 
    has_many :users, :through => :group_memberships 


    accepts_nested_attributes_for :group_memberships 
end 


class GroupMembership < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :collective 

    validates_uniqueness_of :collective_id, :message => "can be only joined once", :scope => 'user_id' 
end 

class User < ActiveRecord::Base 
    validates :user_name, presence: true, length: { minimum: 4, maximum: 20 } 

    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    has_many :collectives, :through => :group_memberships 
    has_many :group_memberships 
end 

集體控制器

class CollectivesController < ApplicationController 
before_action :set_collective, only: [:show, :edit, :update, :destroy] 

def index 
    @collectives = Collective.all 

end 

def show 

    @users_group = @collective.users 
end 

def new 
    @collective = Collective.new 
end 

def create 
    @collective = Collective.new(collective_params) 
    @collective.users << current_user 

    respond_to do |format| 
    if @collective.save 
    format.html { redirect_to @collective, notice: 'Collective was successfully created.' } 
    format.json { render :show, status: :created, location: @collective } 
    else 
    format.html { render :new } 
    format.json { render json: @collective.errors, status: :unprocessable_entity } 
    end 
end 
end 

    private 

def set_collective 
    @collective = Collective.find(params[:id]) 
end 


def collective_params 
    params.require(:collective).permit(:collectivename, :collectivedescription, :createdby) 
end 

指數提取物:

<tbody> 
    <% @collectives.each do |collective| %> 
    <tr> 
    <td><%= collective.collectivename %></td> 
    <td><%= collective.collectivedescription %></td> 
    <td><%= collective.createdby %></td> 
    <td><%= link_to 'Show', collective %></td> 
    <td><%= link_to 'Edit', edit_collective_path(collective) %></td> 
    <td><%= link_to 'Destroy', collective, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
**Here;s where I'd like to put the join button** 
    </tr> 
<% end %> 

回答

1

創建集體成員的新路線join我n您的配置/ routes.rb文件

resources :collectives do 
    member do 
    post 'join' 
    end 
end 

在你collective_controllers.rb你有一個新動作

before_action :set_collective, only: [:show, :edit, :update, :destroy, :join] 

def join 
    @collective.users << current_user 
    @collective.save 
    redirect_to collectives_path 
end 

而且在你看來...

<td><%= link_to 'Destroy', collective, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
<% unless collective.users.include? current_user %> 
    <td><%= link_to 'Join', join_collective_path(collective), method: :post %></td> 
<% end %>