Ruby on rails 部分名称对象不是有效的Ruby标识符
作为Ajax调用的一部分,我正在呈现一个部分,并且得到了一个错误Ruby on rails 部分名称对象不是有效的Ruby标识符,ruby-on-rails,ruby,ajax,ruby-on-rails-4,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.): 我以前没
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的返回值
我还希望有人能在我这样做的时候澄清部分是如何呈现的(即视图的命名约定)
<%= 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
class CartItemsController
destroy.js.erb
$("#cart-subtotal").empty().append('<%= j render partial: @subtotal %>');
$(“#购物车小计”).empty().append(“”);
我的分部的名称是_subtotal.html.erb,位于/views/subtotal/_subtotal.html.erb
有人能看出我在这里做错了什么吗
谢谢
ActionView::Template::Error(部分名称(94.0)不是有效的
Ruby标识符;确保部分名称以下划线开头,
后跟字母、数字和数字的任意组合
下划线。)
问题就在这里@subtotal
保存CartItem.subtotal
的值,该值为94.0。渲染分部时,应指定分部的名称,在本例中,该名称应为小计。因为分部的位置在/views/subtotal
中,所以它应该在这里
("#cart-subtotal").empty().append('<%= j render partial: @subtotal %>');
2) 将零件留在原处,然后
$("#cart-subtotal").empty().append('<%= j render partial: 'subtotal/subtotal' %>');
因此,您将拥有
@subtotal
变量DRY。消息说明了一切。部分文件的名称应以下划线(35;)开头。Iv'e用我的部分名称更新了我的问题try“”
我想这里是$(“#购物车小计”).empty().append(“”)
您应该使用渲染分部:“subtotal”
这是有效的(并且必须将我的分部移动到视图/cart\u items/\u subtotal.html.erb
),我现在的问题是为什么这样做?谢谢你谢谢Hanks@AndreyDeineko这帮了大忙:-)
$("#cart-subtotal").empty().append('<%= j render partial: 'subtotal/subtotal' %>');
before_action :subtotal, only: %i(show destroy)
private
def subtotal
@subtotal ||= CartItem.subtotal
end