ConversationsController#show 中的 ArgumentError -- 参数数量错误(1..2 为 3)

ArgumentError in ConversationsController#show -- wrong number of arguments (3 for 1..2)

使用 Messageable gem 在我的 Ruby on Rails 应用程序中,我可以成功访问新对话页面并选择一个用户发送到,消息的标题,当然还有消息本身的 body。

但是,当我单击发送按钮并重新加载页面时,我收到以下错误消息:

ArgumentError in ConversationsController#show

wrong number of arguments (3 for 1..2)

Extracted source (around line #4):
2  
3  # Returns the Gravatar for the given user.
4  def gravatar_for(user, options = { size: 80 })
5    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
6    size = options[:size]
7    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=# {size}"

是什么导致了这个问题?我在关注 New Relic tutorial for Messageable 时遇到了很多问题。我不确定这个错误是否可能表明存在两个用于两个不同功能和教程的单独 gravatar 方法的问题(users_helper.rb gravatar 方法是为个人资料图片实现的,来自 Hartl 教程;application_helper.rb gravatar 方法是为 Messageable new 实现的遗迹教程)。我不相信两者之间有任何冲突导致错误,因为我还没有能够修复它,但这是一种可能性(?)

无论如何,如果能解决此问题,我们将不胜感激。


=>Rails版本:4.1.2,Ruby版本:2.1.4


app/helpers/users_helper.rb:

module UsersHelper

  # Returns the Gravatar for the given user.
  def gravatar_for(user, options = { size: 80 })
    gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
    size = options[:size]
    gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
    image_tag(gravatar_url, alt: user.username, class: "gravatar")
  end
end

完整回溯:

app/helpers/users_helper.rb:4:in `gravatar_for'
app/views/conversations/show.html.erb:16:in `block in _app_views_conversations_show_html_erb__1169498260752427397_69885071298420'
app/views/conversations/show.html.erb:12:in `_app_views_conversations_show_html_erb__1169498260752427397_69885071298420'
activerecord (4.2.0) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.2.0) lib/active_record/relation/delegation.rb:46:in `each'
actionview (4.2.0) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:143:in `render'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
rack-timeout (0.2.0) lib/rack/timeout.rb:108:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
rack (1.6.0) lib/rack/content_length.rb:15:in `call'
rack (1.6.0) lib/rack/handler/webrick.rb:89:in `service'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

application_helper:

module ApplicationHelper

  # Helper method to easily render Gravatars.
  def gravatar_for(user, size = 30, title = user.username)
    image_tag gravatar_image_url(user.email, size: size), title: title, class: 'img-rounded'
  end
end

_participants.html.erb:

<% conversation.participants.each do |participant| %>
  <% unless participant == current_user %>
    <%= gravatar_for participant %>
  <% end %>
<% end %>

messages_helper.rb:

module MessagesHelper
  def recipients_options
    s = ''
    User.all.each do |user|
      s << "<option value='#{user.id}' data-img-src='#{gravatar_image_url(user.email, size: 50)}'>#{user.name}</option>"
    end
    s.html_safe
  end
end

app/views/conversations/show.html.erb:

<% provide(:title, 'Conversation') %>

<p>Chatting with
  <%= render 'conversations/participants', conversation: @conversation %>
</p>

<div class="panel panel-default">
  <div class="panel-heading"><%= @conversation.subject %></div>

  <div class="panel-body">
    <div class="messages">
      <% @conversation.receipts_for(current_user).each do |receipt| %>
        <div class="media">
          <% message = receipt.message %>
          <div class="media-left">
            <%= gravatar_for message.sender, 45, message.sender.name %>
          </div>

          <div class="media-body">
            <h6 class="media-heading"><%= message.sender.name %>
                says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></h6>
            <%= message.body %>
          </div>
        </div>
      <% end %>
    </div>
  </div>
</div>

在您的 show 模板中有代码:

<%= gravatar_for message.sender, 45, message.sender.name %>

不幸的是,您在两个助手中定义了 gravatar_for - 一个在您的 UsersHelper 中,一个在您的 ApplicationHelper.

看起来 UsersHelper 的定义优先于 ApplicationHelper 的定义。尝试从 UserHelper 中删除一个,以便调用正确的方法。

祝你好运!

编辑

您提到您希望能够自定义 gravatar 大小,并且您当前实施的 UsersHelper#gravatar_for 是可能的。如下更改 show 中的代码:

<%= gravatar_for message.sender, { size: 45 } %>

请注意,我删除了第三个参数,因此它不会导致任何错误。另外我通过了一个 hash 定义 size.

这应该可以解决问题![​​=24=]

希望对您有所帮助!