Rails 4-邀请队友参加项目 [英] Rails 4 - Invite team mates to project

查看:103
本文介绍了Rails 4-邀请队友参加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to add functionality to my Rails 4 app which allows a user (who creates a project) to invite others to join their project team.

我发现本教程对我有所帮助:

I found this tutorial, which I've found helpful: https://coderwall.com/p/rqjjca/creating-a-scoped-invitation-system-for-rails

至此,我进行了以下设置:

To this point, I have the following set up:

用户

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 %>

然后在我的表演(在项目表演中渲染)中,我拥有:

Then in my show (rendered on the projects show) I have:

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

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

In my invites controller, I have:

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 

我不知道要完成这项工作还需要发生什么.

I can't figure out what else needs to happen to make this work.

保存所有内容并尝试邀请电子邮件地址时,出现此错误:

When I save all this and try to invite an email address, I get this error:

undefined method `project' for nil:NilClass

尽管我用来发送邀请的表格显示在项目显示页面上,但还是会发生这种情况.

That happens despite the form I use to send the invite being shown on the projects show page.

推荐答案

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

You need to add project_id and recipient_id to your invite_params, and add the recipient to your form (as a text_field, or hidden field, depending on your use case):

# 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 %>

这篇关于Rails 4-邀请队友参加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆