views:

675

answers:

3

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>

loadshow:

$(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 %>

I'm getting this error:

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

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?

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

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

P.s.

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

Thanks Dan!

alt text

A: 

The reason this isn't working is because you can't have ERB code in javascript files. Anything between <% %> in a javascript file won't be evaluated by Rails.

I'd advise either loading the images with AJAX or simply putting them in the HTML and using jQuery to show or hide them as necessary.

mjaz
Thanks for the heads up, any code examples?
MrThomas
So how do I target the individual class_id?
MrThomas
.js.erb files can most definitely have rails code in them.. this is what erb is for..
Rabbott
Right, but not in plain .js files like the OP was doing.
mjaz
+2  A: 

Of course you can have ERB code in a javascript file, name it xxxx.js.erb and you have access to it all.

Have a look at this link which shows a great example of how to 'ajaxify' links by simple adding a class to them. Once you do this you can class a link, and create a .js.erb file that will contain your javascript (including erb code) that gets called on success. and if you need too access the class_id, you now have the id of the object as well. id="yourobj_<%= @yourobj.id %>"


UPDATE The reason you javascript is failing is because you need to escape your quotes in the line that is throwing the error, like so:

$(".style_image_<%= style.id %>").html("<%= escape_javascript(render(\"show\")) %>");

OR just use single quotes, like so:

$(".style_image_<%= style.id %>").html('%= escape_javascript(render("show")) %>');
Rabbott
I have a js.erb file it still don't work!But I'll check the link Rabbott, thanks!
MrThomas
My ruby code is fine it's just the Javascript.
MrThomas
Its because you have double quotes, you need to escape the quotes around "show" on this line $(".style_image_<%= style.id %>").html("<%= escape_javascript(render("show")) %>");
Rabbott
Has this been tested because it's not working for me, even with a the js.erb file I can not target the the dom class and mypost.id
MrThomas
I tested the fact that this javascript does not throw an error, you HAVE to do this. As far as 'targeting the dom element' we will need to see some of the generated markup (HTML) to confirm that its actually printing the style.id correctly. Also, a snipit of the rails code would be nice so that we can confirm that style is an actual object and is being retrieved correctly.
Rabbott
<div class="style_image_<%= style.id %>" > <%=link_to image_tag(style.cover.pic.url(:small)), style %></div>
MrThomas
are these being loaded with ajax of any kind, if so youll need to use .live('click', callback). IF NOT, here is my thought: You are trying to pick up the click event for link, the problem is that the image is larger than the a tag (for instance, open up firebug in FF and inspect the a tag, i bet you see that it doesn surround the entire image) so what is actually being picked up here is the click event for the image, not the link.
Rabbott
Rab I'm using link_to image_tag, so the image is the link.
MrThomas
I'm using <div class="style_image_<%= style.id%>" >to make the image link have a unique style_image class.Then I'm using $(".style_image_<%= style.id %> a").click(function() {...to target the that unique class.$
MrThomas
No its not, it produces <a href="#"><img src="" /></a> look at your source..
Rabbott
I know what you are using, I'm looking at it.. open firebug, look at the markup produces, use the inspect to see the area that the link tag covers..
Rabbott
I proved a screen print!I changed the class to <div class="style_image<%=style.id%>
MrThomas
If I don't use the style.id in the the class it targets correctly, but it performs the function on all of the classes so I use the style.id to make it unique and it doesn't work! from there.
MrThomas
Please post a screenshot of the exact same thing, but with the style.ids in the markup please.
Rabbott
Sorry uploaded the wrong pic.
MrThomas
based on that screen shot, you would need $(".style_image<%= style.id %>"), there is no underscore after style_image.. typo?
Rabbott
No typo I removed the _ on all files.
MrThomas
A: 

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. write the javascript in the controller and use a link_to_remote.

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

Image_tag link as above!

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;       

});
});

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!

MrThomas