Rails 4 - 邀请队友参与项目

时间:2016-06-04 04:06:42

标签: ruby-on-rails ruby associations

我试图在我的Rails 4应用程序中添加功能,允许用户(创建项目的人)邀请其他人加入他们的项目团队。

我找到了这个教程,我发现它很有用:https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails

到目前为止,我有以下设置:

用户

has_one :profile, dependent: :destroy

资料

belongs_to :user
has_many :teams, foreign_key: "team_mate_id"

  has_many :team_projects, through: :teams, source: :project
  has_many :invitations, :class_name => "Invite", :foreign_key => 'recipient_id'
  has_many :sent_invites, :class_name => "Invite", :foreign_key => 'sender_id'

项目

belongs_to :profile
    has_one :team
    has_many :team_mates, through: :team
    has_many :invites

邀请

 belongs_to :project
      belongs_to :sender, :class_name => 'Profile'
      belongs_to :recipient, :class_name => 'Profile'

belongs_to :project
  belongs_to :team_mate, class_name: "Profile"

在我的表格中,我有:

<%= simple_form_for(@invite, :url => invites_path) do |f| %>
            <%= f.hidden_field :project_id, :value => @invite.project_id %>
            <%= f.label :email %>
            <%= f.email_field :email %>
            <%= f.input :expiry, :as => :date_picker, :label => "When do you need a response to this invitation?"  %>

            <%= f.submit 'Send' %>
            <% end %>

然后在我的节目中(在项目节目中呈现)我有:

<%= render :partial => 'projects/invite_team_mate'  %>

在我的邀请控制器中,我有:

class InvitesController < ApplicationController

    def new
      @invite = Invite.new
    end

    def create
      @invite = Invite.new(invite_params)
      @invite.sender_id = current_user.profile.id
      if @invite.save

        #if the user already exists
        if @invite.recipient != nil 

           #send existing user email invitation to join project team
           InviteMailer.existing_user_invite(@invite).deliver 

           #Add the user to the user group - inivte rsvp pending
           @invite.recipient.project.push(@invite.project)
        else
            #send new user email invitation to join as a user and this project team
           @invite.recipient.project.push(@invite.project)

           # InviteMailer.new_user_invite(@invite, new_user_registration_path(:invite_token => @invite.token)).deliver
        end
      else
         # oh no, creating an new invitation failed
      end
    end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_invite
      @invite = Invite.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def invite_params
      params[:invite].permit(:email)
    end   
end 

我无法弄清楚还需要做些什么来实现这项工作。

当我保存所有这些并尝试邀请电子邮件地址时,我收到此错误:

undefined method `project' for nil:NilClass

尽管我用来发送邀请的表单显示在项目展示页面上,但仍然会发生这种情况。

3 个答案:

答案 0 :(得分:1)

您需要将project_idrecipient_id添加到您的invite_params,并将收件人添加到您的表单(作为text_field或隐藏字段,具体取决于您的使用案例):

# controller
def invite_params
  params[:invite].permit(:email, :project_id, :recipient_id)
end   

# form
<%= simple_form_for(@invite, :url => invites_path) do |f| %>
  ...
  <%= f.hidden_field :recipient_id, :value => get_recipient_id %>
  ...
<% end %>

答案 1 :(得分:0)

错误是由@ invite.project_id引起的,因为@invite没有数据所以它会抛出错误

<%= f.hidden_field :project_id, :value => @invite.project_id %>

用或用其他一些所需的逻辑替换它

select_tag "people", options_from_collection_for_select(@projects, "id", "name")

在控制器中

def new
  @invite = Invite.new
  @projects = current_user.team_projects // here you have to add your logic, for which project you want to invite or let me know 
end

答案 2 :(得分:0)

我发现以下代码很奇怪

if @invite.recipient != nil 
    ...
   #Add the user to the user group - inivte rsvp pending
   @invite.recipient.project.push(@invite.project)
else
    #send new user email invitation to join coalfacer and this project team
   @invite.recipient.project.push(@invite.project)
   ...
end

你如何调用相同的代码@ invite.recipient。即使@ invite.recipient是Nil?!

顺便说一句,确保您理解为什么在控制器中编写此代码,这意味着什么

def invite_params
  params[:invite].permit(:email)
end   

为方便起见,请不要复制您不了解的代码。此外,即使您这样做,也可以分小部分进行尝试,然后尝试每一个,这样您就可以将错误本地化(如果有的话)。以小增量工作至关重要。你不能做大量的改变,然后只是问他的大量代码有什么不对,并且#34;

最后,我建议您使用MCVE原则撰写具体问题。您必须提取与问题相关的代码的特定部分,并具体针对该问题。如果你把一大堆代码(包括不相关的代码)放在一边,那就更难以帮助了。