简体   繁体   English

Javascript Rails模板无法正常工作

[英]Javascript Rails template doesn't work properly

I try to make an Ajax-based solution for web-store. 我尝试为网络商店制作一个基于Ajax的解决方案。 So I have the Cart, and when user adds something, I want not to have all page reloaded, but only "Cart" template. 所以我有了购物车,当用户添加东西时,我不想重新加载所有页面,而只希望重新加载“购物车”模板。 (Line Item belongs to Cart) Create method in Line Item: (订单项属于购物车)在订单项中创建方法:

#usualstuff
respond_to do |format|
if @line_item.save
format.html { redirect_to(store_url) }
format.js
format.xml { render :xml => @line_item,
:status => :created, :location => @line_item }
else
...

Put the create.js file in /views/line_items 将create.js文件放在/ views / line_items中

So here is the problem. 所以这就是问题所在。

Actually it works. 确实有效。 But when I click the button "Add to Cart", nothing changes, untill I reload page. 但是,当我单击“添加到购物车”按钮时,什么都没有改变,直到我重新加载页面。

On server log is shown: 在服务器日志上显示:

Rendered line_items/create.js (0.0ms)

0.0ms -for sure means something is wrong, but I can't figuare this out. 0.0毫秒-肯定意味着出了点问题,但是我无法确定这一点。 Looks like rails just opens it and does nothing. 看起来像导轨只是打开它,什么也不做。

What may be the reason? 可能是什么原因?

Code in create.js create.js中的代码

page.replace_html('cart' , render(@cart))

you still need to write the actual ajax request in javascript or jquery to call the method on the controller to update the display. 您仍然需要使用javascript或jquery编写实际的Ajax请求,以调用控制器上的方法来更新显示。

ruby is a server side language and so you need to make an ajax call to the server to get the data you wish to use to update your page. ruby是一种服务器端语言,因此您需要对服务器进行ajax调用以获取要用于更新页面的数据。

check this post out they seem to be doing something similar to what you are it may be of some use. 看看这篇文章 ,他们似乎在做些与您实际相似的事情,这可能有用。 if your unsure on what ajax is have a look at this link . 如果您不确定是什么ajax,请查看此链接

hope this helps somewhat. 希望这会有所帮助。

因此.js / .rjs模板在Rails> 3.0中不起作用,应为.js.erb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM