:remote =>真实的Ajax调用,在Rails中不起作用

时间:2022-10-08 14:15:02

I'm following the RailsCast video on jQuery and Ajax. I've added the :remote => true line to the link_to. In the video, it says that nothing should happen but the link still goes to the page.

我正在跟踪关于jQuery和Ajax的RailsCast视频。我添加了:remote => true行到link_to。在视频中,它说什么都不应该发生,但是链接仍然指向页面。

jQuery is being loaded (I'm using it for other things in my Rails app).

jQuery正在被加载(我在Rails应用程序中使用它做其他事情)。

I'm trying to link to two different resources (things that I've generated through scaffolds) which might be the problem. I'm not sure.

我试图链接到两个不同的资源(我通过搭建生成的东西),这可能是问题所在。我不确定。

Let me know if you need to see more code.

如果需要查看更多代码,请告诉我。

EDIT

编辑

I had the jquery_ujs line after some previous JS. I've moved it to the top and the remote link now does not go to the page. However, with my new.js.erb file, I can't seem to get the form to appear when the link is clicked.

在之前的一些JS之后,我有了jquery_ujs行。我已经将它移动到顶部,远程链接现在不会转到页面。然而,随着我的new.js。erb文件,当点击链接时,我似乎无法显示窗体。

Link code:

链接代码:

<%= link_to 'New Project', new_project_path, :id => "new-project-link", :remote => true %>

new.js.erb:

new.js.erb:

$('#new-project-link').hide().after('<%= j render("form") %>');

Also, I'm getting a header status of 304 (Not Modified) instead of a 200. Does that matter at all?

另外,我获得了304(未修改)的标题状态,而不是200。这有关系吗?

1 个解决方案

#1


3  

Make sure you've include csrf_meta_tags in the head section of your layout:

确保在布局的头部部分包含csrf_meta_tags:

http://apidock.com/rails/ActionView/Helpers/CsrfHelper/csrf_meta_tags

http://apidock.com/rails/ActionView/Helpers/CsrfHelper/csrf_meta_tags

Also, I'd check that your respond_to block (for js) is set up correctly in the controller action (Projects#new), and also be sure the request is a GET. I think it will be a POST by default per jquery_ujs.

另外,我将检查您的respond_to block(用于js)是否在controller操作(Projects#new)中正确设置,并确保请求是GET。我认为它将是默认的每个jquery_ujs的POST。

#1


3  

Make sure you've include csrf_meta_tags in the head section of your layout:

确保在布局的头部部分包含csrf_meta_tags:

http://apidock.com/rails/ActionView/Helpers/CsrfHelper/csrf_meta_tags

http://apidock.com/rails/ActionView/Helpers/CsrfHelper/csrf_meta_tags

Also, I'd check that your respond_to block (for js) is set up correctly in the controller action (Projects#new), and also be sure the request is a GET. I think it will be a POST by default per jquery_ujs.

另外,我将检查您的respond_to block(用于js)是否在controller操作(Projects#new)中正确设置,并确保请求是GET。我认为它将是默认的每个jquery_ujs的POST。