Tôi đang theo cuốn sách Pragmatic Phát triển Web Agile Với Rails 4th Edition, NHƯNG Tôi đang sử dụng Rails 3.2.2 thay vì 3.0.5 theo khuyến cáo trong cuốn sách:Rails 3.2.2 không thực hiện RJS
~$ ruby -v
ruby 1.9.3p125 (2012-02-16) [i686-linux]
~$ rails -v
Rails 3.2.2
Tôi gặp khó khăn khi đưa AJAX vẽ lại Giỏ hàng mà không cần tải lại trang. Đây là hành động tạo ra trong 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
Và đây là create.js.rjs tôi tập RJS (dưới app/views/line_items):
page.alert('NO PROBLEM HERE')
page.replace_html('cart', render(@cart))
Tuy nhiên, khi tôi nhấp vào nút để bắt đầu hành động này:
<%= button_to 'Add to Cart', line_items_path(:product_id => product), :remote => true %>
tôi nhận được lỗi sau trong nhật ký phát triển:
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'
Nếu tôi thay đổi tên tập tin của create.js.rjs để create.js.erb, vấn đề được khắc phục:
Rendered line_items/create.js.erb (0.4ms)
nhưng có gì xảy ra trong giao diện .... thậm chí không cảnh báo. Tôi đang thiếu gì? Sự khác nhau giữa file.js.erb và file.js.rjs là gì?
hey guy! Tôi đã tìm thấy bài đăng của bạn trên google. Tôi cũng phải đối mặt với tình huống tương tự. Bạn đã tìm ra giải pháp chưa? – code4j
Tôi đã khắc phục sự cố !!! Xem [giải pháp] của tôi (http://stackoverflow.com/questions/12220816/the-ajax-request-cannot-see-the-effect-without-refresh-the-browser-in-rails/12224196#12224196). Tôi hy vọng điều này có thể giúp bạn. – code4j