RJS adds comment but doesn't update form


#1

Hi,

When a user adds a comment i want to add the comment to the end of the
comments list using ajax so there is no full page refresh. The code i
have below adds the comment to the database but doesn’t update the
comments div with the new comment.

Can anyone help? This is my first use of rjs templates and i have read
http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates

I am using eclipse with rad rails on windows for development and i am
running rails 1.1.

post.rhtml
<%= render :partial => “post”, :object => @post %>


<%= render :partial => "comment", :collection => @post.comments %>

<p<%= render :partial => ‘newcomment’, :id => @post %>

_comment.rhtml

<%= comment.name %> Says:

<%= comment.comment %>

<%= comment.created_at.to_s(:long) %>


blog_controller

def comment
Post.find(params[:id]).comments.create(params[:comment])
render :update do |page|
# show the new comment at the top of the comments display
page.insert_html(:top, “comments”, :partial => “comment”)
# quick and dirty way to empty the form that was just submitted
page.replace_html(“commentform”, :partial => “commentform”)
end


#2

John B. wrote:

Hi,

When a user adds a comment i want to add the comment to the end of the
comments list using ajax so there is no full page refresh. The code i
have below adds the comment to the database but doesn’t update the
comments div with the new comment.

Can anyone help? This is my first use of rjs templates and i have read
http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates

I am using eclipse with rad rails on windows for development and i am
running rails 1.1.

post.rhtml
<%= render :partial => “post”, :object => @post %>


<%= render :partial => "comment", :collection => @post.comments %>

<p<%= render :partial => ‘newcomment’, :id => @post %>

_comment.rhtml

<%= comment.name %> Says:

<%= comment.comment %>

<%= comment.created_at.to_s(:long) %>


blog_controller

def comment
Post.find(params[:id]).comments.create(params[:comment])
render :update do |page|
# show the new comment at the top of the comments display
page.insert_html(:top, “comments”, :partial => “comment”)
# quick and dirty way to empty the form that was just submitted
page.replace_html(“commentform”, :partial => “commentform”)
end

You’re lacking an “end” to your render :update call.

Indent the code under render :update and it will be easier to see, but
you call render :update do |page| without an end statement.

I’ve been finding RJS tricky to debug since it fails so silently.

Jeff


#3

Jeff C.man wrote:

John B. wrote:

blog_controller

def comment
Post.find(params[:id]).comments.create(params[:comment])
render :update do |page|
# show the new comment at the top of the comments display
page.insert_html(:top, “comments”, :partial => “comment”)
# quick and dirty way to empty the form that was just submitted
page.replace_html(“commentform”, :partial => “commentform”)
end

def comment
Post.find(params[:id]).comments.create(params[:comment])
render :update do |page|
# show the new comment at the top of the comments display
page.insert_html(:top, “comments”, :partial => “comment”)
# quick and dirty way to empty the form that was just submitted
page.replace_html(“commentform”, :partial => “commentform”)
end
end

This should do it.

If you still have troubles, one thing to try is to simplify your RJS
calls to eliminate potential problems in your partials or with your
syntax. For example, to try a simple

page.replace_html “This is a test”

in place of a partial call that doesn’t seem to be working. If you see
“This is a test”, then your syntax is working and you can narrow it down
to a possible error in the partial.

Hope this helps,

Jeff


#4

then you have a problem somewhere else. the ‘end’ closes the ‘render
:update’ block


#5

I followed your advice with the page.replace_html “This is a test” and
it turned out there was a problem with the partial.

I have used the following code below to render the post and comments
separetly but i will refactor this to make it add the comment to the end
of the comment list and update the no of comments on the end of the post
improving performane.

The extra end you suggested causes a syntax error but removing it
works?? Seems strange.

def comment
Post.find(params[:id]).comments.create(params[:comment])
@post = Post.find(params[:id])
render :update do |page|
page.replace_html( “postdiv”, :partial => “post”, :id => @post)
page.replace_html(“commentlistdiv”, :partial => “commentlist”,
:object => @post)
end

Thanks


#6

John B. wrote:

I followed your advice with the page.replace_html “This is a test” and
it turned out there was a problem with the partial.

I have used the following code below to render the post and comments
separetly but i will refactor this to make it add the comment to the end
of the comment list and update the no of comments on the end of the post
improving performane.

The extra end you suggested causes a syntax error but removing it
works?? Seems strange.

def comment
Post.find(params[:id]).comments.create(params[:comment])
@post = Post.find(params[:id])
render :update do |page|
page.replace_html( “postdiv”, :partial => “post”, :id => @post)
page.replace_html(“commentlistdiv”, :partial => “commentlist”,
:object => @post)
end

Thanks

I don’t know what’s going on in your code, but the snippet you posted is
missing an “end” for its “do”.

The “render :update” command takes a block as its parameter, and a block
is formed by “do … end”.

What you have there is:

def comment

render :update do |page|

end

See the problem?

You need

def comment

render :update <block_goes_here>
end

like so:

def comment

render :update do |page|

end
end

If what you posted is working, then you have an extra “end” somewhere
else or there’s some other kind of problem with your syntax.

If you’re having a problem with a partial, what I do is to go into the
partial and make it as simple as possible, start with something like

_post.rhtml

This is _post.rhtml

and then try it. If it you see “This is _post.rhtml”, then try adding
an instance variable:

controller:
def comment
@post = “This is a post”
render :update do |page|
page.replace_html “postdiv”, :partial => “post”
end
end

_post.rhtml:

<%= @post %>

and so on. Then when you see “This is a post”, you know that the
variable is making it into your partial, and you can replace it with the
proper assignment.

Another problem you may be having is the :id parameter in your partial
call–you might try taking it out and just referring to @post in your
partial.

I’ve had problems even sending local variables to partials from render
:update, so your best best is to just assign the variable in your
controller and use it in the partial.

Jeff