Forum: Ruby on Rails RJS adds comment but doesn't update form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (johnnybutler7)
on 2006-04-11 09:42
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/rail...

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 %>
<hr/>

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

<p<%= render :partial => 'newcomment',  :id => @post %></p>
<div>

_comment.rhtml
<div id="divcomment">
        <p><%= comment.name %> Says:</p>
        <p><%= comment.comment %></p>
        <p id="dateformat"><%= comment.created_at.to_s(:long) %></p>
        <hr/>
</div>

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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-11 09:46
John Butler 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/rail...
>
> 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 %>
> <hr/>
>
> <div id="comments">
> <%= render :partial => "comment", :collection => @post.comments %>
>
> <p<%= render :partial => 'newcomment',  :id => @post %></p>
> <div>
>
> _comment.rhtml
> <div id="divcomment">
>         <p><%= comment.name %> Says:</p>
>         <p><%= comment.comment %></p>
>         <p id="dateformat"><%= comment.created_at.to_s(:long) %></p>
>         <hr/>
> </div>
>
> 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
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-11 09:52
Jeff Coleman wrote:
> John Butler 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
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (johnnybutler7)
on 2006-04-11 11:33
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-04-11 13:50
(Received via mailing list)
then you have a problem somewhere else.  the 'end' closes the 'render
:update' block
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-11 18:30
John Butler 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

<p>This is _post.rhtml</p>

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:

<p><%= @post %></p>

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
This topic is locked and can not be replied to.