簡體   English   中英

在郵件程序中使用設計方法“ current_user”

[英]Using devise method 'current_user' in mailer

對Rails來說相對較新,我已經使用devise認證gem了一段時間,但是我的問題是,我的應用程序發送了一封電子郵件,填寫了表單后顯示了當前用戶的名字和姓氏,但是每次我嘗試要使用上述參數發送電子郵件,我會收到一條錯誤消息,指出“未定義的方法current_user”,我的郵件程序看起來像這樣,請注意:它是一個單獨的控制器來控制它。

<table>
<tr>
<td>name</td><td><%= current_user.firstname %><%=current_user.firstname  %></td>

<tr>

        <td>phone no.</td><td><%= current_user.phone %></td>

    </tr>

    <tr>

        <td>location</td><td><%= @device.location %></td>

    </tr>

    <tr>

        <td>device type</td><td><%= @device.device_type %></td>

    </tr>

    <tr>

        <td>brand</td><td><%= @device.brand %></td>

    </tr>

</table>

我的控制器動作是這樣

def create

    @user=current_user

    @device = Device.new(device_params)

        if @device.save

            DeviceMailer.send_device.deliver

            flash[:notice] = 'Request sent successfully,you will be

很快就聯系上了” else flash [:error] ='請求未發送,請檢查詳細信息並再次發送'渲染'新'端

end

current_user是僅對視圖和控制器可用的方法。 如果要在郵件程序或模型中使用它,則必須將其顯式用作郵件程序方法的參數。

在控制器內部的create方法:

DeviceMailer.send_device(current_user, @device).deliver

郵寄方式:

def send_device(user, device)
  @user = user
  @device = device
  # More code as needed
end

郵件視圖:

<table>
  <tr>
    <td>name</td>
    <td><%= @user.firstname %><%= @user.firstname  %></td>
  </tr>
  <tr>
    <td>phone no.</td><td><%= @user.phone %></td>
  </tr>
  <tr>
    <td>location</td><td><%= @device.location %></td>
  </tr>
  <tr>
    <td>device type</td><td><%= @device.device_type %></td>
  </tr>
  <tr>
    <td>brand</td><td><%= @device.brand %></td>
  </tr>
</table>

登錄時設置current_user。 為了在用戶執行控制器操作之前強制用戶登錄,您需要在控制器頂部具有以下行:

before_action :authenticate_user!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM