【问题标题】:Rails 6: undefined method `with_indifferent_access' for "{}":StringRails 6:“{}”的未定义方法`with_indifferent_access':字符串
【发布时间】:2020-10-12 20:03:59
【问题描述】:

我在将项目部署到 Heroku 时遇到问题。在我的本地主机中运行它可以正常工作,但我已部署给我这个错误:

我正在使用 public_activity gem 来获取在我的应用中完成的最后一个活动的提要。

2020-06-22T18:00:31.055655+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b] ActionView::Template::Error (undefined method `with_indifferent_access' for "{}":String):
2020-06-22T18:00:31.055655+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b]     1: <% @activities.each do |activity| %>
2020-06-22T18:00:31.055656+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b]     2: <div class="activity">
2020-06-22T18:00:31.055656+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b]     3:     <%= activity.owner.name if activity.owner %>
2020-06-22T18:00:31.055656+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b]     4:     <%= render_activity activity %>
2020-06-22T18:00:31.055657+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b]     5: </div>
2020-06-22T18:00:31.055657+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b]     6: <% end %>
2020-06-22T18:00:31.055658+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b]   
2020-06-22T18:00:31.055658+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b] app/views/activities/index.html.erb:4
2020-06-22T18:00:31.055658+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b] app/views/activities/index.html.erb:1
2020-06-22T18:00:31.055671+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b] app/controllers/concerns/users/time_zone.rb:19:in `block in set_time_zone'
2020-06-22T18:00:31.055671+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b] app/controllers/concerns/users/time_zone.rb:19:in `set_time_zone'
2020-06-22T18:00:31.055672+00:00 app[web.1]: [3e23fc99-a452-4cb4-900d-087df67a3f6b] lib/jumpstart/lib/jumpstart/account_middleware.rb:30:in `call'
2020-06-22T18:00:31.056486+00:00 app[web.1]: I, [2020-06-22T18:00:31.056422 #4]  INFO -- : [3e23fc99-a452-4cb4-900d-087df67a3f6b] Processing by ErrorsController#internal_server_error as HTML
2020-06-22T18:00:31.057546+00:00 app[web.1]: I, [2020-06-22T18:00:31.057488 #4]  INFO -- : [3e23fc99-a452-4cb4-900d-087df67a3f6b]   Rendering errors/internal_server_error.html.erb within layouts/errors
2020-06-22T18:00:31.057825+00:00 app[web.1]: I, [2020-06-22T18:00:31.057762 #4]  INFO -- : [3e23fc99-a452-4cb4-900d-087df67a3f6b]   Rendered errors/internal_server_error.html.erb within layouts/errors (Duration: 0.2ms | Allocations: 42)
2020-06-22T18:00:31.058712+00:00 app[web.1]: I, [2020-06-22T18:00:31.058652 #4]  INFO -- : [3e23fc99-a452-4cb4-900d-087df67a3f6b]   Rendered shared/_favicons.html.erb (Duration: 0.0ms | Allocations: 6)
2020-06-22T18:00:31.058933+00:00 app[web.1]: I, [2020-06-22T18:00:31.058882 #4]  INFO -- : [3e23fc99-a452-4cb4-900d-087df67a3f6b] Completed 500 Internal Server Error in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms | Allocations: 810)

我正在使用的活动控制器是这个:

class ActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_account.id, recipient_type: "Account")
  end
end

然后我在创建、更新和销毁保存后的帖子和讨论控制器中有这些行:

@post.create_activity :create, owner: current_user, recipient: current_account

app/views/index.html.erb 中的代码

<% @activities.each do |activity| %>
<div class="activity">
    <%= activity.owner.name if activity.owner %>
    <%= render_activity activity %>
</div>
<% end %>

和 app/controllers/concerns/users/time_zone.rb

module Users
  module TimeZone
    extend ActiveSupport::Concern

    included do
      helper_method :browser_time_zone
      around_action :set_time_zone
    end

    def browser_time_zone
      browser_tz = ActiveSupport::TimeZone.find_tzinfo(cookies[:browser_time_zone])
      ActiveSupport::TimeZone.all.find { |zone| zone.tzinfo == browser_tz } || Time.zone
    rescue TZInfo::UnknownTimezone, TZInfo::InvalidTimezoneIdentifier
      Time.zone
    end

    def set_time_zone
      if user_signed_in? && current_user.time_zone?
        Time.use_zone(current_user.time_zone) { yield }
      else
        yield
      end
    end
  end
end

来自渲染活动的代码,public_activity gem 从文件夹views/public_activity/post/_create.html.erb 中获取它 看起来像这样:

added a comm

<% if activity.trackable %>
<%= link_to activity.trackable.title, activity.trackable  %>
<% else %>
which has since been removed
<% end %>

【问题讨论】:

  • 能否把app/views/activities/index.html.erb和app/controllers/concerns/users/time_zone.rb的内容也加进去。
  • 我已经添加了他们知道@ChristianBruckmayer
  • 请提供来自render_activity 助手的代码。
  • 添加了一个渲染模板。它在帮助程序中没有任何代码,只是从视图中的 public_activity 文件夹中获取视图。在那里我为我想要活动的每个模型添加文件夹。

标签: ruby-on-rails activerecord rubygems actionview


【解决方案1】:

我在这里找到了答案(或至少是一种解决方法):

https://github.com/chaps-io/public_activity/issues/345

当我检查时,我看到 Heroku 将 parameters 保存为字符串 '{}' 而不是空哈希。将此行添加到循环顶部为我修复了它:

<% @activities.each do |activity| %>
  <% activity.parameters = {} if activity.parameters == '{}' %>

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多