Rails 4 - Messenger gem - 教程设置

时间:2016-01-12 08:12:35

标签: ruby-on-rails messenger

我正在尝试在Rails 4中创建一个应用程序。

我试图按照本教程设置messenger gem。 http://josephndungu.com/tutorials/private-inbox-system-in-rails-with-mailboxer

我有:

的Gemfile:

gem "mailboxer"

User.rb

def mailboxer_name
  self.full_name
end

def mailboxer_email(object)
  self.email
end

mailboxer.rb初始化

config.email_method = :mailboxer_email
config.name_method = :full_name

我的用户表中有一个名为:email的属性,我的用户模型中有一个名为:

的方法
def full_name
  if first_name.present?
    [*first_name.capitalize, last_name.capitalize].join(" ")
  else 
    test full name
  end    
end

此尝试与教程之间的一个区别是,由于我之前尝试过网站点教程(并且无法使其正常工作),我的应用程序中已经有一个消息控制器和视图文件夹。本教程使用“邮箱”的地方,我使用了'邮件'

我无法超越本教程中的第一个测试点。我收到这个错误:

NoMethodError in MessagesController#inbox
undefined method `inbox' for #<ActiveRecord::Associations::CollectionProxy []>

错误消息突出显示了此方法:

def inbox
  @inbox = messages.inbox
  @active = :inbox
end

我有:

消息控制器

class MessagesController < ApplicationController
  before_action :authenticate_user!

  def inbox
    @inbox = messages.inbox
    @active = :inbox
  end

  def sent
    @sent = messages.sentbox
    @active = :sent
  end

  def trash
    @trash = messages.trash
    @active = :trash
  end
end

应用程序助手

def flash_class(level)
  case level.to_sym
    when :notice then "alert alert-success"
    when :info then "alert alert-info"
    when :alert then "alert alert-danger"
    when :warning then "alert alert-warning"
  end
end

def active_page(active_page)
  @active == active_page ? "active" : ""
end

消息助手

def unread_messages_count
  # how to get the number of unread messages for the current user
  # using mailboxer
  messages.inbox(:unread => true).count(:id, :distinct => true)
end

消息视图文件夹具有:

_folder_view.html.erb

<div class="row">
  <div class="spacer"></div>
  <div class="col-md-12">
    <!-- we'll configure this to compose new conversations later -->
    <%= link_to "Compose", "#", class: "btn btn-success" %>
    <div class="spacer"></div>
  </div>

  <div class="col-md-4">
    <div class="panel panel-default">
      <div class="panel-body">
        <%= render 'messages/folders' %>
      </div>
    </div>
  </div>

  <div class="col-md-8">
    <div class="panel panel-default">
      <div class="panel-body">
        <!-- individual conversations will show here -->
      </div>
    </div>
  </div>

</div>
view raw

_folders.html.erb

<ul class="nav nav-pills nav-stacked">
  <li class="<%= active_page(:inbox) %>">
    <%= link_to messages_inbox_path do  %>
        <span class="label label-danger pull-right"><%=unread_messages_count%></span>
        <em class="fa fa-inbox fa-lg"></em>
        <span>Inbox</span>
    <% end %>
  </li>
  <li class="<%= active_page(:sent) %>">
    <%= link_to messages_sent_path do %>

        <em class="fa fa-paper-plane-o fa-lg"></em>
        <span>Sent</span>
    <% end %>
  </li>

  <li class="<%= active_page(:trash) %>">
    <%= link_to messages_trash_path do %>
        <em class="fa fa-trash-o fa-lg"></em>
        <span>Trash</span>
    <% end %>
  </li>
</ul>

inbox.html.er,sent.html.erb和trash.html.erb

中的每一个
<%= render partial: 'messages/folder_view' %>

的routes.rb

get "messages/inbox" => "messages#inbox", as: :messages_inbox
  get "messages/sent" => "messages#sent", as: :messages_sent
  get "messages/trash" => "messages#trash", as: :messages_trash

应用程序控制器:

helper_method :messages

  private


  def messages
    @messages ||= current_user.messages
  end

谁能看到我出错的地方?

我可以按照教程步骤说明:点击收件箱链接可以带您进入我们的启动并运行的收件箱页面,其中已经为收件箱,已发送和垃圾箱文件夹提供了导航。

相反,我收到一条错误消息:

NoMethodError in MessagesController#inbox
undefined method `inbox' for #<ActiveRecord::Associations::CollectionProxy []>

任何人都可以看到错误吗?

1 个答案:

答案 0 :(得分:0)

您忘记将辅助方法消息添加到教程中的 ApplicationController

class ApplicationController < ActionController::Base
  # [...]
  helper_method :messages

  private

  def messages
    @messages ||= current_user.mailbox
  end

  protected

  # [...]
end

更新1

我在你的助手中看到你有current_user。消息 in应该是current_user。邮箱