ruby-on-rails - 部分名称对象不是有效的 Ruby 标识符

标签 ruby-on-rails ruby ajax ruby-on-rails-4

作为我的 Ajax 调用的一部分,我正在渲染部分内容,但出现错误

 ActionView::Template::Error (The partial name (94.0) is not a valid Ruby identifier; make sure your partial name starts with underscore, and is followed by any combination of letters, numbers and underscores.):

我以前没见过这个,也不确定它是如何生成的。在这种情况下,94 是@subtotal 的返回值

我也希望有人能在我做的时候澄清部分是如何呈现的(即 View 的命名约定)

<%= j render partial: @object %>

所以在我的例子中,我有一个部分保存用户购物车中项目的小计

class CartItemsController < ApplicationController
  def show
    @subtotal = CartItem.subtotal
  end

  def destroy
    @cart_item = CartItem.find(params[:id])
    @cart_item.destroy
    respond_to do |format|
      if @cart_item.destroy
        @subtotal = CartItem.subtotal
        format.js { flash.now[:success] = 'Successfully removed from cart'  }
      else
        format.js { flash.now[:error] = 'Sorry, Something went wrong' }
      end
  end
end

销毁.js.erb

$("#cart-subtotal").empty().append('<%= j render partial: @subtotal %>');

我的部分名称是_subtotal.html.erb,位于/views/subtotal/_subtotal.html.erb

谁能看看我这里有没有做错什么?

谢谢

最佳答案

ActionView::Template::Error (The partial name (94.0) is not a valid Ruby identifier; make sure your partial name starts with underscore, and is followed by any combination of letters, numbers and underscores.)

问题就在这里'<%= j render partial: @subtotal %>' . @subtotal持有 CartItem.subtotal 的值(value)即 94.0。当您呈现部分时,您应该指定部分的名称,在您的情况下应该是小计 .由于部分的位置在 /views/subtotal 中,应该是"<%= j render partial: 'subtotal/subtotal' %>"

关于ruby-on-rails - 部分名称对象不是有效的 Ruby 标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34650530/

相关文章:

ruby-on-rails - Rails - 包括静态 HTML 片段

css - Rails 4 图像在生产中更大

ruby-on-rails - Ruby On Rails - 如何重命名当前上传的文件?

javascript - 如何检测位置哈希的变化?

ruby-on-rails - 用户个人资料应该是单独的模型吗?

ruby-on-rails - rails rspec update_all 方法在 Controller 中有效,但在 rspec 中无效

ruby - Bundler 吐出来自其他项目和 Gemfiles 的错误

css - 如何从使用 twitter-Bootstrap-rails gem 切换到使用普通 Bootstrap

jquery - 仍然收到 "Request format is unrecognized for URL unexpectedly ending in..."

jQuery ajax 问题,堆叠 div