我正在關注的書務實敏捷Web開發使用Rails第四版,但我用Rails 3.2.2,而不是3.0.5在書中建議:的Rails 3.2.2不執行RJS
~$ 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(在應用程序/視圖/ 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有什麼不同?
嘿傢伙!我在谷歌上找到你的帖子。我面臨同樣的情況。你找到解決方案嗎? – code4j 2012-08-31 20:09:39
我修復了這個問題!看到我的[解決方案](http://stackoverflow.com/questions/12220816/the-ajax-request-cannot-see-the-effect-without-refresh-the-browser-in-rails/12224196#12224196)。我希望這可以幫助你。 – code4j 2012-09-01 01:32:37