【发布时间】:2012-04-26 05:58:11
【问题描述】:
我正在关注Pragmatic Agile Web Development With Rails 4th Edition这本书,但我使用的是 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 有什么区别?
【问题讨论】:
-
嘿家伙!我在谷歌上找到了你的帖子。我面临同样的情况。你找到解决办法了吗?
-
我解决了这个问题!!!见我的solution。我希望这可以帮助你。
标签: javascript ruby-on-rails ruby ajax rjs