2016-03-15 67 views
1

我正在使用Slack-Notifier gem將Web鉤子添加到我的rails應用程序中。用鬆弛通知gem添加slack web hook到rails應用程序

我想每次在數據庫中創建一個新用戶時觸發一個Web鉤子。

我有以下,但我認爲我的語法是關閉的。我在我的IDE中出現以下錯誤。

enter image description here

def create 
    @user = User.new(user_params) 
    if @user.save 
     @user.send_activation_email 
     flash[:info] = "Welcome to Pallet! Please check your email to activate your account." 
     SLACK_NOTIFIER.ping("New Directory User: #{@user: @user.email, @user.discipline}", username: 'notifier', icon_emoji: ':confetti_ball:") 
     redirect_to charges_path 
    else 
     render 'new' 
    end 
    end 

配置/初始化/ slack.rb

require 'slack-notifier' 

    SLACK_NOTIFIER = Slack::Notifier.new("https://hooks.slack.com/services/xxxxxx", 
    channel: '#directory-bot', 
    username: 'notifier' 
) 

回答

1

總而言之,這一行看起來有問題的:

SLACK_NOTIFIER.ping("New Directory User: #{@user: @user.email, @user.discipline"}, username: 'notifier', icon_emoji: ':confetti_ball:") 

我不知道是什麼用意這一行是,但是這行有語法錯誤。例如,

  1. 閉合大括號應該是在雙引號內的字符串插值。

  2. 此外,@user將轉化爲紅寶石對象。所以我想你可能會給而不是@user:

  3. 當你寫下@user.email, @user.discipline的時候,你想要一個數組嗎?你需要明確地把它們放在[]

不確定整個上下文,但我認爲還有其他問題。

+0

謝謝。這看起來更好嗎? 'SLACK_NOTIFIER.ping(「New Directory User:#{@ user},#{@user.email},#{@user.discipline}」, username:'notifier',icon_emoji:':confetti_ball:') – rs19

0

如果用單引號打開一個字符串,則也必須用單引號將其關閉。

SLACK_NOTIFIER.ping("New Directory User: #{@user: @user.email, @user.discipline"}, username: 'notifier', icon_emoji: ':confetti_ball:') 
                               SINGLE QUOTE HERE TOO^

如果打開(,你必須關閉它:

需要「鬆弛通知」

SLACK_NOTIFIER = Slack::Notifier.new("https://hooks.slack.com/services/xxxxxx", 
    channel: '#directory-bot', 
    username: 'notifier' 
) 

有在代碼中更多的錯誤只是這兩個,我是不會指出它們,因爲Stack Overflow不是語法驗證的地方。

真的,真的需要Ruby中的速成課來學習基本語法。

+0

謝謝那些只是錯別字。我現在在這裏'SLACK_NOTIFIER.ping(「新目錄用戶:#{@ user},#{@user.email},#{@user.discipline}」, 用戶名:'notifier',icon_emoji:':confetti_ball :')' – rs19