2017-08-07 45 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 %>