【发布时间】:2016-01-07 08:35:28
【问题描述】:
我在 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 的返回值
我也希望有人能在我这样做时澄清部分是如何呈现的(即视图的命名约定)
<%= 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
destroy.js.erb
$("#cart-subtotal").empty().append('<%= j render partial: @subtotal %>');
我的部分名称是_subtotal.html.erb,位于/views/subtotal/_subtotal.html.erb
谁能看看我这里有没有做错什么?
谢谢
【问题讨论】:
-
消息说明了一切。部分文件的名称应以下划线 (_) 开头。
-
我用我的部分名称更新了我的问题
-
试试
"<%= j render partial: 'subtotal/subtotal' %>" -
我认为这里
$("#cart-subtotal").empty().append('<%= j render partial: @subtotal %>');你应该使用render partial: 'subtotal' -
这行得通(并且不得不将我的部分转移到
views/cart_items/_subtotal.html.erb),我现在的问题是为什么它行得通?谢谢
标签: ruby-on-rails ruby ajax ruby-on-rails-4