Topic: Problem with jquery .html or .load

Hi. I'm new on RoR. I've searched the forum for this issue, and although I found similar things, I did not find my answer.

I've been trying to make a div reload after a new post is made. The post is inserted in the database, I'm using format.js so that it goes through my create.js.erb file.

I have my create.js.erb file, and it stops working when I have this line added:

$('#posts-div').html("<%= escape_javascript(render :partial => 'posts', :collection => @posts) %>");

By my testing, it's not accepting the % character, if I \escape it it just writes the text on the Div instead of interpreting as code.


I've tried using .load instead of .html and it works with any file on application, but not with my _posts.html.erb partial.

I know this may be one of already know issues, missing gems, or other, but I've spent so many hours on this that I feel dumb.

Please help me, I'm tired or googling, and I'm almost sure it's something really really simple that I'm missing.

Thank you

Re: Problem with jquery .html or .load

It looks like you should take a look at your _posts.html.erb file first. Check if your code there is valid, even (or especially) missing ' or " matters. 
Open Firebug and in console write:

$('#posts-div').html("Hey, it is a string")

and run it.
Do you see the text in #posts-div?

Re: Problem with jquery .html or .load

Hi, thank you for your reply.

Yes, if I write just some text it successfully changes the div's content to that text.
It just doesn't work when there's a <% tag.

All the jquery on the create.js.erb is working correctly if I use some text instead of RoR code.

Everything else is working, I've triple checked.

Re: Problem with jquery .html or .load

Your problem is you are sending embedded ruby code directly to the browsers JavaScript interpreter, JavaScript doesn't know how to parse embedded ruby.

The only thing you can put in the .html call is valid HTML

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Problem with jquery .html or .load

Hi Brad.

I had thought about that, this probably only works if put in a view.

Can you help me with .load() ?

I tried it too and it worked for any file, it would render inside the div. But, and this is where your expertise comes in, it wouldn't run all the code.

The partial file is doing something like:

<% @posts.each do |post| %>

And going through all the posts and outputting them. So what's my problem?

I'm calling the .load() after doing a remote insert of a post, and as such, before the code above I was inserting this:

<% @posts = Posts.where(just an example) %>

I was trying to update this variable from the database before showing the posts again, do that it would show the post that was just inserted.


Thank you for your time.

Re: Problem with jquery .html or .load

Are you using Rails 2.X or 3.X?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Problem with jquery .html or .load

Rails 3.X, most recent version.

Re: Problem with jquery .html or .load

See if this helps

http://railscasts.com/episodes/205-unob … javascript

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Problem with jquery .html or .load

I've found my problem, and it's entirely my fault.

I forgot to mention I wanted to call a partial from another controller, I went through the server log and saw the missing file error.
So as soon as I changed the code to:

$('#posts-div').html("<%= escape_javascript(render 'users/posts') %>");

It started working.

Sorry for wasting your time, thank you for all your help.

Re: Problem with jquery .html or .load

A good case for posting more context when you have a problem,  I'd say as a rule of thumb, never post the ONE line of code you are having problems with,  post the entire action or method,  or entire view, etc.  Usually you should post several snippets,  i.e. view and related controller or controller action.

I assumed you were hand coding jquery in plain old HTML!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.