简体   繁体   English

在Rails 3中使用“ Beginning Rails 3”的jQuery

[英]jQuery in Rails 3 with “Beginning Rails 3”

I'm learning how to use Rails with "Beginning Rails 3" by Carneiro and Barazi. 我正在学习如何在Carneiro和Barazi的“ Beginning Rails 3”中使用Rails。 The end goal is to build a functional blog. 最终目标是建立一个实用的博客。 This book is oriented towards Rails 3.0 and I'm running 3.2, so I am using jQuery while they are using Prototype for Ajax stuff. 本书面向Rails 3.0,并且我正在运行3.2,因此我在使用Prototype处理Ajax时使用jQuery。 At one point they ask you to switch from Processing to jQuery, which I skipped. 有一次他们要求您从Processing切换到jQuery,但我跳过了。 Later, when they start to use js code, they have you put this code 后来,当他们开始使用js代码时,他们让您放了这段代码

<%= render @article %>
<h3>Comments</h3>
<div id="comments">
  <%= render @article.comments %>
</div>
<%= link_to "new comment", new_article_comment_path(@article, :format => :js), :remote => true, :id => 'new_comment_link' %>

into the "article" view, and this code 进入“文章”视图,然后这段代码

$("<%= escape_javascript render(:file => 'comments/new.html.erb') %>").insertAfter('#comments');
$('#new_comment_link').hide();

into a javscript file in the new_comment view that works when creating new comments. 放入new_comment视图中的javscript文件中,该文件在创建新注释时起作用。 The idea is that when you display an article, it shows all of the comments and a link to create a new comment. 其想法是,当您显示文章时,它会显示所有评论以及用于创建新评论的链接。 When you click the link, it loads the new_comment fields from the new_comment view, instead of having them there all the time. 当您单击链接时,它将从new_comment视图中加载new_comment字段,而不是一直将它们放在那里。 However, when I load my blog and click the "new comment" link, nothing happens. 但是,当我加载博客并单击“新评论”链接时,没有任何反应。 I've tried following the instructions from the book (put in the jquery.min.js file, putting in a jQuery adaptor) but it doesn't seem to work (and shouldn't be put in to begin with, I think). 我尝试按照书中的说明进行操作(放入jquery.min.js文件,放入jQuery适配器),但它似乎不起作用(我认为不应将其放在开头) 。 Any ideas? 有任何想法吗? Help would be much appreciated. 帮助将不胜感激。

1) Have you included the jquery-rails gem? 1)您是否包含了jquery-rails gem? It helps wire jquery up to rails, and you don't need to manually add the jquery.min.js file yourself 它有助于将jquery固定在轨道上,并且您不需要自己手动添加jquery.min.js文件

2) Have you tried putting an alert in the javascript file to make sure it's running at all? 2)您是否尝试过将警报放入javascript文件中以确保其完全运行?

3) Have you checked your development log to see what's happening when it hits that new comment url to see what it says? 3)您是否检查过开发日志以查看当它击中新的注释URL时会发生什么情况?

I've spent the last three days debugging a similar issue[1]:http://stackoverflow.com/questions/9666929/simple-rails-ajax-call-wont-process-as-js 我已经花了三天时间调试类似的问题[1]:http://stackoverflow.com/questions/9666929/simple-rails-ajax-call-wont-process-as-js

First, Rails 3.2 uses a gem. 首先,Rails 3.2使用了gem。 Put it in your gemfile and run bundle install. 将其放在您的gemfile中并运行bundle install。

Second (as the page says) you'll need to make sure both included, though they were set by default: In app/assets/javascripts/application.js You should have 其次(如页面所示),尽管它们都是默认设置的,但您需要确保两者都包括在内:在app / assets / javascripts / application.js中,您应该

//= require jquery
//= require jquery_ujs

Note that jquery-ujs actually does much of the magic here. 请注意,jquery-ujs实际上在这里起到了很多作用。

Two things can break: 可能会打破两件事:

  1. Your controller may not receive the ajax request properly 您的控制器可能未正确收到ajax请求
  2. your response may not work 您的回应可能无效

To see which is the case, look at your server output or log. 若要查看情况,请查看服务器输出或日志。 Does it show that new_article_comment is being executed? 是否表明new_article_comment正在执行? And does it say "Processing ... as JS"? 并显示“正在处理...作为JS”吗?

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

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