2015-08-16 44 views
0

我有模型Invintation和模型公司,只有在確認消息邀請後,公司才能向對方發送消息。如何在RoR中顯示已確認的邀請?

Invintation.rb

class Invintation < ActiveRecord::Base 
    belongs_to :recipient, class_name: 'Company', foreign_key: 'recipient_id' 
    belongs_to :sender, class_name: 'Company', foreign_key: 'sender_id' 
    belongs_to :author, class_name: 'User', foreign_key: 'author_id' 
end 

Company.rb

class Company < ActiveRecord::Base 
    has_many :users_companies 
    has_many :users, through: :users_companies 
    has_many :sent_messages, class_name: 'Message', foreign_key: 'sender_id' 
    has_many :incoming_messages, class_name: 'Message', foreign_key: 'recipient_id' 
    has_many :sent_invitations, class_name: 'Invintation', foreign_key: 'sender_id' 
    has_many :invitation_recipients, through: :sent_invitations, source: :recipient 
    has_many :incoming_invitations, class_name: 'Invintation', foreign_key: 'recipient_id' 
    has_many :invitation_senders, through: :incoming_invitations, source: :sender 
end 

在表單我想展示公司誰確認邀請

<%= form_for([@company, @message]) do |f| %> 
    <%= f.text_field :subject, placeholder: "тема" %> 
    <%= f.collection_select :category_id, Category.all, :id, :name, class: "form-control" %> 
    <%= f.collection_select :recipient_id, @recipients, :id, :name, class: "form-control" %> 
    <%= f.submit %> 
<% end %> 

所以我添加到控制器該代碼

class MessagesController < ApplicationController 

    def new 
    @company = Company.find(params[:company_id]) 
    @message = @company.sent_messages.new 
    @recipients = @company.invitation_recipients.where(confirm: true) 
    end 

    def create 
    @company = Company.find(params[:company_id]) 
    @message = @company.messages.build(mess_params) 
    @message.author_id = current_user 
    @message.sender_id = @company.id 
    if @message.save 
     flash[:success] = "Документы успешно отправлены" 
     redirect_to inbox_path(@company) 
    end 
    end 
end 

但我不知道我可以在@recipients只顯示確認的公司,我哪裏出錯了?

回答

0

我不確定這是否是最有效的方式,但我會爲聯繫公司制定邀請和模型。當一家公司邀請另一家公司時,會創建邀請。當另一家公司接受邀請被銷燬並創建公司之間的鏈接時。然後只顯示公司選擇接收來自鏈接公司的消息。