Rails 3:简单的 AJAXy 页面更新?

发布于 2024-09-29 03:39:10 字数 336 浏览 4 评论 0原文

我不敢相信我为了这个简单的任务花了四个小时,但我做到了。

在 Rails 2.3 中,我可以使用以下简单代码替换页面的一部分:

render :update do |page| page.replace_html "div_id", :partial => “新内容”,... 在

Rails 3 中,Ryan Bates 让我编写全新的 javascript 函数,从 Prototype(rails 默认)切换到 jQuery,否则就不享受生活了。其他课程也同样简单。

我缺少什么?现在我们如何替换

I can't believe I've been looking four hours for this simple task, but I have.

In Rails 2.3, I could replace one section of a page with this simple code:

render :update do |page|
page.replace_html "div_id", :partial => "new_content",...
end

In Rails 3, Ryan Bates has me writing entire new javascript functions, switching from Prototype (rails default) to jQuery, and otherwise not enjoying life. The other tutes are no more straightforward.

What am I missing? How do we replace a <div> these days?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

您的好友蓝忘机已上羡 2024-10-06 03:39:10

谢谢,伙计们。官方的答案似乎是,是的,团队认为简单是优秀的敌人,因此将其变得更加复杂。

第一个关键是创建一个 .js.erb 文件,该文件名为调用 ajax 更新的方法。因此,如果索引方法处理更新,请将原始 JavaScript 放入 index.js.erb 中。这位于视图文件夹中。

其次,在index.js.erb中工作的代码是

m = $('list_users');    
m.innerHTML = "<%= escape_javascript(render :partial => "reload_users") %>";

然后进行调用,在控制器方法的respond_to块中添加:

format.js

最后,调用视图有:

<%= link_to "Update User List", @reload_users_path, :remote => true %>

顺便说一句,据说所有旧页面都使用page.replace 将起作用。插件下载页面表明它在 Rails 3 的最新版本中出现问题并且尚未修复。此外,如果您使用它,各种博主会来到您家并更换您。

Thanks, guys. The official answer seems to be that, yes, the team felt simple is the enemy of good and made it more complicated.

The first key is to create a .js.erb file NAMED for the method CALLING the ajax update. So if the index method handles the update, put the raw javascript in index.js.erb. This goes in the views folder.

Second, the code that worked in index.js.erb was

m = $('list_users');    
m.innerHTML = "<%= escape_javascript(render :partial => "reload_users") %>";

Then to make the call, add in the respond_to block of the controller method, add:

format.js

Finally, the calling view has:

<%= link_to "Update User List", @reload_users_path, :remote => true %>

By the way, supposedly all the old pages using page.replace will work if you install a plugin. The plugin download page suggests that it broke in the last releases of Rails 3 and has not been fixed. Also, various bloggers will come to your home and birch-switch you if you use it.

∞琼窗梦回ˉ 2024-10-06 03:39:10

整个 RJS 内容使 javascript 内联,并使 dom 非常引人注目。此外,通过避免内联 javascript,您可以通过在浏览器中压缩缓存这些文件来开辟其他可能的优化 javascript 的方法。这就是为什么 RJS 超出了 Rails 3 范围的原因。花一天时间使用 jQuery 或 Prototype 应该能让你熟悉这些小东西,并且从长远来看将有助于项目。

The whole RJS stuff makes the javascript inline and makes the dom very obtrusive. Also, by avoiding inline javascript you could open up other possible ways of optimizing you javascript by compressing and caching those files in browser. Thats the reason why RJS is getting out of scope from rails 3. A little bit of getting around with jQuery or Prototype for a day should get you on gears with these kind of small stuff and will help the project on long run.

人事已非 2024-10-06 03:39:10

你那里还有 jQuery 吗?我会在任何一天推荐它而不是 Prototype...

如果它仍然存在,您可以在 Javascript 中使用以下内容:

$.get("<%= url_for path/to/partial %>",
      function(response) {
        $("#div_id").html(response);
      });

这通过 AJAX 获取部分,然后将其转储到 ID 为 div_id 的 div 中。

希望这有帮助!

Do you still have jQuery in there? I'd recommend it over Prototype any day...

If it's still there you can just use the following in your Javascript:

$.get("<%= url_for path/to/partial %>",
      function(response) {
        $("#div_id").html(response);
      });

This gets the partial via AJAX and just dumps it into the div with id div_id.

Hope this helps!

谎言 2024-10-06 03:39:10

我什至不确定您是否需要进行 AJAX 调用来加载该部分。我相信在 js.erb 文件中,对 render(:partial => object_or_path) 的调用只会返回一个包含所有 html 的字符串,您可以将其包装在 jQuery 对象中并附加。例子:

$('#div_id').html($('<%= render :partial => @object %>'))

I'm not even sure you need to make an AJAX call to load that partial. I believe that in a js.erb file, a call to render(:partial => object_or_path) will just return a string with all the html, which you can wrap in a jQuery object and append. Example:

$('#div_id').html($('<%= render :partial => @object %>'))
花想c 2024-10-06 03:39:10

据我所知,沿着与上面答案相同的路线,您可以在模板中执行类似的操作:

<%= link_to "Update User List", @reload_users_path, :remote => true %>

在控制器中,执行以下操作:

respond_to do |format|
  format.js {
    render :text => "alert('reloaded')"
  }
end

这样您就可以让控制器“执行”客户端 JS,就像渲染一样:update 过去常做的事。这相当于在 Rails 2 中执行以下操作:

render :update do |page|
   page << "alert('reloaded')"
end

有什么理由不建议这种方法吗?

As far as I know, along the same line as the answer above, you can do something like this in your template:

<%= link_to "Update User List", @reload_users_path, :remote => true %>

And in controller, do this:

respond_to do |format|
  format.js {
    render :text => "alert('reloaded')"
  }
end

This way you can have controller "execute" client side JS much the same as as render :update used to do. This is equivalent to doing the following in Rails 2:

render :update do |page|
   page << "alert('reloaded')"
end

Is there any reason why this approach is not advisable?

心作怪 2024-10-06 03:39:10

试试这个:

page.call "$('#div_id').html", render(:partial => 'new_content')

Try this:

page.call "$('#div_id').html", render(:partial => 'new_content')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文