作为我的 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/