gpt4 book ai didi

ruby-on-rails - 无方法错误 : undefined method `url_for' for nil:NilClass

转载 作者:可可西里 更新时间:2023-11-01 11:36:50 29 4
gpt4 key购买 nike

我遇到了很奇怪的问题。
Rails 在 url_for 上提示:

NoMethodError: undefined method `url_for' for nil:NilClass

完整回溯:

/gems/actionpack-3.2.18/lib/action_dispatch/routing/url_for.rb:148 in url_for
/gems/actionpack-3.2.18/lib/action_view/helpers/url_helper.rb:107 in url_for
/gems/actionpack-3.2.18/lib/action_dispatch/routing/route_set.rb:216 in reports_market_path
app/views/reports/market/index.html.erb:7 in _app_views_reports_market_index_html_erb___1902231581999475126_70184101502700
/gems/actionpack-3.2.18/lib/action_view/template.rb:145 in block in render
/gems/activesupport-3.2.18/lib/active_support/notifications.rb:125 in instrument
/gems/actionpack-3.2.18/lib/action_view/template.rb:143 in render
/gems/actionpack-3.2.18/lib/action_view/renderer/template_renderer.rb:47 in block (2 levels) in render_template
/gems/actionpack-3.2.18/lib/action_view/renderer/abstract_renderer.rb:38 in block in instrument
/gems/activesupport-3.2.18/lib/active_support/notifications.rb:123 in block in instrument
/gems/activesupport-3.2.18/lib/active_support/notifications/instrumenter.rb:20 in instrument
/gems/activesupport-3.2.18/lib/active_support/notifications.rb:123 in instrument
/gems/actionpack-3.2.18/lib/action_view/renderer/abstract_renderer.rb:38 in instrument
/gems/actionpack-3.2.18/lib/action_view/renderer/template_renderer.rb:46 in block in render_template
/gems/actionpack-3.2.18/lib/action_view/renderer/template_renderer.rb:54 in render_with_layout
/gems/actionpack-3.2.18/lib/action_view/renderer/template_renderer.rb:45 in render_template
/gems/actionpack-3.2.18/lib/action_view/renderer/template_renderer.rb:18 in render
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:145 in block in render_with_newrelic
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/method_tracer.rb:69 in block in trace_execution_scoped
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/method_tracer_helpers.rb:82 in trace_execution_scoped
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/method_tracer.rb:67 in trace_execution_scoped
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:144 in render_with_newrelic
/gems/actionpack-3.2.18/lib/action_view/renderer/renderer.rb:36 in render_template
/gems/actionpack-3.2.18/lib/action_view/renderer/renderer.rb:17 in render
/gems/actionpack-3.2.18/lib/abstract_controller/rendering.rb:110 in _render_template
/gems/actionpack-3.2.18/lib/action_controller/metal/streaming.rb:225 in _render_template
/gems/actionpack-3.2.18/lib/abstract_controller/rendering.rb:103 in render_to_body
/gems/actionpack-3.2.18/lib/action_controller/metal/renderers.rb:28 in render_to_body
/gems/actionpack-3.2.18/lib/action_controller/metal/compatibility.rb:50 in render_to_body
/gems/actionpack-3.2.18/lib/abstract_controller/rendering.rb:88 in render
/gems/actionpack-3.2.18/lib/action_controller/metal/rendering.rb:16 in render
/gems/actionpack-3.2.18/lib/action_controller/metal/instrumentation.rb:40 in block (2 levels) in render
/gems/activesupport-3.2.18/lib/active_support/core_ext/benchmark.rb:5 in block in ms
/home/deploy/.rbenv/versions/1.9.3-p484/lib/ruby/1.9.1/benchmark.rb:295 in realtime
/gems/activesupport-3.2.18/lib/active_support/core_ext/benchmark.rb:5 in ms
/gems/actionpack-3.2.18/lib/action_controller/metal/instrumentation.rb:40 in block in render
/gems/actionpack-3.2.18/lib/action_controller/metal/instrumentation.rb:83 in cleanup_view_runtime
/gems/activerecord-3.2.18/lib/active_record/railties/controller_runtime.rb:24 in cleanup_view_runtime
/gems/actionpack-3.2.18/lib/action_controller/metal/instrumentation.rb:39 in render
/gems/active_scaffold-3.3.3/lib/active_scaffold/extensions/action_controller_rendering.rb:14 in render_with_active_scaffold
/gems/actionpack-3.2.18/lib/action_controller/metal/implicit_render.rb:10 in default_render
/gems/actionpack-3.2.18/lib/action_controller/metal/implicit_render.rb:5 in send_action
/gems/actionpack-3.2.18/lib/abstract_controller/base.rb:167 in process_action
/gems/actionpack-3.2.18/lib/action_controller/metal/rendering.rb:10 in process_action
/gems/actionpack-3.2.18/lib/abstract_controller/callbacks.rb:18 in block in process_action
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:469 in _run__801877313021617769__process_action__2065492084595526813__callbacks
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:405 in __run_callback
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:385 in _run_process_action_callbacks
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:81 in run_callbacks
/gems/actionpack-3.2.18/lib/abstract_controller/callbacks.rb:17 in process_action
/gems/actionpack-3.2.18/lib/action_controller/metal/rescue.rb:29 in process_action
/gems/actionpack-3.2.18/lib/action_controller/metal/instrumentation.rb:30 in block in process_action
/gems/activesupport-3.2.18/lib/active_support/notifications.rb:123 in block in instrument
/gems/activesupport-3.2.18/lib/active_support/notifications/instrumenter.rb:20 in instrument
/gems/activesupport-3.2.18/lib/active_support/notifications.rb:123 in instrument
/gems/actionpack-3.2.18/lib/action_controller/metal/instrumentation.rb:29 in process_action
/gems/actionpack-3.2.18/lib/action_controller/metal/params_wrapper.rb:207 in process_action
/gems/activerecord-3.2.18/lib/active_record/railties/controller_runtime.rb:18 in process_action
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:23 in block in process_action
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:353 in perform_action_with_newrelic_trace
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/rails3/action_controller.rb:22 in process_action
/gems/actionpack-3.2.18/lib/abstract_controller/base.rb:121 in process
/gems/actionpack-3.2.18/lib/abstract_controller/rendering.rb:45 in process
/gems/actionpack-3.2.18/lib/action_controller/metal.rb:203 in dispatch
/gems/actionpack-3.2.18/lib/action_controller/metal/rack_delegation.rb:14 in dispatch
/gems/actionpack-3.2.18/lib/action_controller/metal.rb:246 in block in action
/gems/actionpack-3.2.18/lib/action_dispatch/routing/route_set.rb:73 in call
/gems/actionpack-3.2.18/lib/action_dispatch/routing/route_set.rb:73 in dispatch
/gems/actionpack-3.2.18/lib/action_dispatch/routing/route_set.rb:36 in call
/gems/journey-1.0.4/lib/journey/router.rb:68 in block in call
/gems/journey-1.0.4/lib/journey/router.rb:56 in each
/gems/journey-1.0.4/lib/journey/router.rb:56 in call
/gems/actionpack-3.2.18/lib/action_dispatch/routing/route_set.rb:608 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/rack/agent_hooks.rb:30 in traced_call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/rack/browser_monitoring.rb:23 in traced_call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/warden-1.2.3/lib/warden/manager.rb:35 in block in call
/gems/warden-1.2.3/lib/warden/manager.rb:34 in catch
/gems/warden-1.2.3/lib/warden/manager.rb:34 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/best_standards_support.rb:17 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-1.4.5/lib/rack/etag.rb:23 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-1.4.5/lib/rack/conditionalget.rb:25 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/head.rb:14 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/params_parser.rb:21 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/flash.rb:242 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:210 in context
/gems/rack-1.4.5/lib/rack/session/abstract/id.rb:205 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/cookies.rb:341 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/activerecord-3.2.18/lib/active_record/query_cache.rb:64 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/activerecord-3.2.18/lib/active_record/connection_adapters/abstract/connection_pool.rb:479 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/callbacks.rb:28 in block in call
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:405 in _run__2176267765707103723__call__2820452351963234767__callbacks
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:405 in __run_callback
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:385 in _run_call_callbacks
/gems/activesupport-3.2.18/lib/active_support/callbacks.rb:81 in run_callbacks
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/callbacks.rb:27 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-1.4.5/lib/rack/sendfile.rb:102 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/remote_ip.rb:31 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/debug_exceptions.rb:16 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/show_exceptions.rb:56 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/railties-3.2.18/lib/rails/rack/logger.rb:32 in call_app
/gems/railties-3.2.18/lib/rails/rack/logger.rb:16 in block in call
/gems/activesupport-3.2.18/lib/active_support/tagged_logging.rb:22 in tagged
/gems/railties-3.2.18/lib/rails/rack/logger.rb:16 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/actionpack-3.2.18/lib/action_dispatch/middleware/request_id.rb:22 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-1.4.5/lib/rack/methodoverride.rb:21 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-1.4.5/lib/rack/runtime.rb:17 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-1.4.5/lib/rack/lock.rb:15 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/rack-cache-1.2/lib/rack/cache/context.rb:136 in forward
/gems/rack-cache-1.2/lib/rack/cache/context.rb:245 in fetch
/gems/rack-cache-1.2/lib/rack/cache/context.rb:185 in lookup
/gems/rack-cache-1.2/lib/rack/cache/context.rb:66 in call!
/gems/rack-cache-1.2/lib/rack/cache/context.rb:51 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/gems/railties-3.2.18/lib/rails/engine.rb:484 in call
/gems/railties-3.2.18/lib/rails/application.rb:231 in call
/gems/railties-3.2.18/lib/rails/railtie/configurable.rb:30 in method_missing
/gems/newrelic_rpm-3.11.2.286/lib/new_relic/agent/instrumentation/middleware_tracing.rb:67 in call
/usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:94 in process_request
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:149 in accept_and_process_next_request
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110 in main_loop
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:414 in block (3 levels) in start_threads
/usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:111 in block in create_thread_and_abort_on_exception

出现错误的代码示例。
views/reports/index.html.erb(第 7 行)

    <% content_for :head do %>
<%= javascript_include_tag 'reports/market/index' %>
<% end %>

<h1>Market Report</h1>

7. <%= form_tag reports_market_path(params[:id] || -1), method: :get do -%>
Start:
<br/>
<input class='report-datepicker' type="text" value="<%=Time.now.strftime("%Y-%m-%d")%>" class="date-input" name="begin_time" />
<br/>
End:
<br/>
<input class='report-datepicker' type="text" value="<%=Time.now.strftime("%Y-%m-%d")%>" class="date-input" name="end_time" />
<br/>
Franchise:
<br/>
<%= select 'franchise', 'id', Franchise.name_hash(current_user.accessible_franchises).merge({'All' => -1}), :selected => params[:id] || -1 %>
<br/>
<%= submit_tag 'generate', name: nil %>
<%end%>

错误是逐个出现的。刷新几页后,它可能会消失。错误出现在存在路由路径的不同页面上。
所以行<%= form_tag reports_market_path(params[:id] || -1), method: :get do -%>抛出错误。

可能的解决方法:
a) index.html由ActiveScaffold Controller 渲染。
b) 有一个名为 Notifier 的邮件程序具有报告 操作:

class Notifier < ActionMailer::Base
def report(to, report, template, domain)
@logo = domain.logo

av = ActionView::Base.new()
av.view_paths = ActionController::Base.view_paths
av.class.class_eval do
include Rails.application.routes.url_helpers
include ApplicationHelper
end

html = av.render(:template => template, :locals => {:report => report, :logo => @logo})

kit = PDFKit.new(html, :page_size => 'A3', :margin_top=>'0.25in', :margin_right=>'0.25in', :margin_bottom=>'0.25in', :margin_left=>'0.25in')
kit.stylesheets << File.join(Rails.root,'public/stylesheets/bootstrap.css')
kit.stylesheets << File.join(Rails.root,'app/assets/stylesheets/admin/admin.css')

attachments[report.filename] = {
mime_type: "application/pdf",
content: kit.to_pdf
}

mail(subject: report.name, to: to, from: '...', date: Time.now) do |format|
format.text { render text: 'Pls, see attached PDF document' }
format.html { render text: html }
end
end

该操作将 View 内容读取为 html 并将其传递给 PDF 创建引擎。
我有一个假设(也很奇怪)在添加代码之后:

av = ActionView::Base.new()
av.view_paths = ActionController::Base.view_paths
av.class.class_eval do
include Rails.application.routes.url_helpers
include ApplicationHelper
end

错误开始出现在应用程序中。
c) 我们添加了 Redis 作为缓存存储。

 config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { expires_in: 90.minutes, namespace: 'production' }

d) Nginx 服务器重启后错误消失但过一段时间又出现。

非常感谢。

最佳答案

所以,我找到了导致奇怪问题的代码。

av.class.class_eval do
include Rails.application.routes.url_helpers
include ApplicationHelper
end

我不确定它为什么会导致这个问题,可能它是线程不安全的,但是当我删除它时一切都正常了。
非常接近的问题 Active Model Serializers : undefined method `url_for' for nil:NilClass

关于ruby-on-rails - 无方法错误 : undefined method `url_for' for nil:NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31852138/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com