【问题标题】:'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path'nil' 不是 ActiveModel 兼容的对象。它必须实现 :to_partial_path
【发布时间】:2015-04-21 17:39:46
【问题描述】:

当我尝试查看任何页面时,我收到上述错误。

这是我的 layouts/application.html.erb 代码:

<body>
<%= link_to "Store Front", root_path, id: "logo" %>
  <div class="container">
    <div class="row">
        <aside class="span4">
        </aside> 
    </div>
    <%= yield %>
    <%= render 'layouts/header' %>
    <div class="container">
      <div class="row">
        <div class="span8">
          <%= yield %>
        </div>
        <aside class="span4">
          <%= render @cart %>
        </aside>  
      <%= render 'layouts/footer' %>
    </div>
</body>

问题是由 引起的。

这是我的 application_controller :

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  include SessionsHelper
  helper_method :current_order

  private

  def current_cart
      Cart.find(session[:cart_id])
  rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      session[:cart_id] = cart.id
      cart
  end
end

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    application_controller 中使用before_filter :current_cart

    并将current_cart 更改为

    def current_cart
        @cart = Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      @cart = Cart.create
      session[:cart_id] = @cart.id
      @cart
    end
    

    愿这些改变能奏效。

    【讨论】:

    • 完美,你做到了!非常感谢
    猜你喜欢
    • 2018-08-04
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多