我正在关注这本书使用 Rails 进行实用敏捷 Web 开发第 4 版,但我使用的是 Rails 3.2.2,而不是书中推荐的 3.0.5:
~$ ruby -v
ruby 1.9.3p125 (2012-02-16) [i686-linux]
~$ rails -v
Rails 3.2.2
当我使用 AJAX 来重新绘制购物车而不重新加载页面时,我陷入了困境。这是 line_items_controller.rb 中的创建操作:
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to(store_url) }
format.js
format.json { render json: @line_item, status: :created, location: @line_item }
else
format.html { render action: "new" }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
这是我的 RJS 文件 create.js.rjs (位于 app/views/line_items 下):
page.alert('NO PROBLEM HERE')
page.replace_html('cart', render(@cart))
但是,当我单击启动此操作的按钮时:
<%= button_to 'Add to Cart', line_items_path(:product_id => product), :remote => true %>
我在开发日志中收到以下错误:
ActionView::MissingTemplate (Missing template line_items/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/home/me/src_rails/depot/app/views"
):
app/controllers/line_items_controller.rb:47:in `create'
如果我将 create.js.rjs 的文件名更改为 create.js.erb,问题就会得到纠正:
Rendered line_items/create.js.erb (0.4ms)
但视图中没有任何反应......甚至没有警报。
我缺少什么?
file.js.erb 和 file.js.rjs 有什么区别?