Rails Ajax jQuery问题 [英] Rails ajax jquery problem

查看:85
本文介绍了Rails Ajax jQuery问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有这个问题,我正在尝试使用Jquery加载部分替换列出的对象.

Ok I have this problem I'm trying to use Jquery to load a partial in replace of a listed object.

index.html.erb

<div class="style_image_<%= style.id %>" > <%=link_to image_tag(style.cover.pic.url(:small)), style %></div>

加载节目:

$(function() {
    $(".style_image_<%= style.id %> a").click(function() {
    $(".style_image_<%= style.id %>").html("loading... ")
    $(".style_image_<%= style.id %>").html("<%= escape_javascript(render("show")) %>")
    $.get(this.href, null, null, "html");
    return false;
  });
});

_show.html.erb:

<%=link_to image_tag(style.cover.pic.url(:normal)), style %>

我遇到此错误:

missing ) after argument list
[Break on this error] $(".style_image_<%= style.id %>").htm...scape_javascript(render("show")) %>")\n

我的代码有两个问题,第一个是click函数未定位.style_image_<%= style.id%> ....即(.style_image_42)如果我将CSS目标替换为42而不是_style.id,点击目标有效; 这是为什么?

There is two problems with my code here the first is the click function is not targeting the .style_image_<%= style.id %> .... i.e (.style_image_42) if I replace the css target with 42 instead of _style.id the click target works; why is this?

无论是否进行此更改,都不会呈现_show部分,并且会给出上述错误.

And with or without this change the _show partial is not render and the above error is given.

使用Javascript并不是很好,任何帮助都会很棒!

Not really that good with Javascript any help would be great!

Ps.

我真正想要的效果就像那些超级酷的货运主题之一: http://cargocollective.com/publikspace

The effect I really want is like one of those super cool cargo themes: http://cargocollective.com/publikspace

谢谢丹!

alt text http://s3.amazonaws.com/data.tumblr.com/tumblr_kzu1cnOUUs1qbto6oo1_1280.png?AWSAccessKeyId=0RYTHV9YYQ4W5Q3HQMG2&Expires=1269597925&Signature=XuwwBhKKCKu3FWGpmqXmBqwxzS0%3D

推荐答案

仍然不确定为什么我的js.erb文件不理解<%= style.id%>,也许不是在这里要做的事情.

Still not sure why my js.erb file don't understand <%=style.id%>, maybe it's not the done thing to do here.

对于这个问题,我有两种解决方法:

I have two solutions for this problem:

  1. 在控制器中编写JavaScript并使用link_to_remote.

  1. write the javascript in the controller and use a link_to_remote.

毫不干扰地将链接路径加载到链接的DIV中:

Unobtrusively Load link path in to the DIV of the link:

上面的Image_tag链接!

Image_tag link as above!

并在我的Application.js文件中

and in my Application.js file

$(function() {

$(".style_image a").live('click', function(event) { 

    $(this).closest(".style_teaser").load(this.href + " #show_photo");


    return false;       

});
});

使用这种方法,如果javascript处于关闭状态,则链接将使您无法到达对象的显示路径.

With this method if javascript is off link will tak you to the show path of the object.

感谢您来到尼克·克拉弗给我这个提示!

Thanks goes out to Nick Craver for give me the heads up on this!

这篇关于Rails Ajax jQuery问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆