Forum: Ruby on Rails Rendering partial and js together

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.
330edb8dc8bbc5a824984cf46e941b2f?d=identicon&s=25 Tam Kbe (tkbeili)
on 2009-03-30 06:21
Hello,

I have a form on my page that is popup-like but actually part of the
page. it works fine as I used in other page. In order to close it after
submission. I do something like
        respond_to do|wants|
            wants.js do
              render :update do |page|
                page << "jq('#message_title, #message_body').val('');"
                page << "tb_remove()"
              end
            end
          end

As requirements change I was to update another DIV with listing so I
would like to do something like
render :partial => 'client_listing', :locals => {:listing_clients =>
@listing_clients}

I need to do both together but I can as you can only render once per
method with Rails:
actionController::DoubleRenderError in SmsController#add_client

Can only render or redirect once per action

Any ideas how I can approach this issue as I need to perform both
actions?

Thanks,

Tam
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-03-30 17:03
(Received via mailing list)
Hi Tam,

On Mon, 2009-03-30 at 06:21 +0200, Tam Kbe wrote:
>               end
> actionController::DoubleRenderError in SmsController#add_client
>
> Can only render or redirect once per action
>
> Any ideas how I can approach this issue as I need to perform both
> actions?

If I understand you correctly, you can accomplish what you're trying to
do by using the RJS replace_html method within the render :update.
Assuming there's an element already on the page within which you'd like
to render the partial above...

render :update do |page|
  page << "jq('#message_title, #message_body').val('');"
  page << "tb_remove()"
  page.replace_html 'existing_element', :partial => 'client_listing',
:locals => {:listing_clients => @listing_clients}
end

The 'replace_html' method will replace the html content inside
'existing_element'.

There's also a straight 'replace' method which replaces the html content
AND the enclosing element.

I highly recommend Cody Fauser's 'RJS Templates' PDF available at
O'Reilly.  Very good expenditure of $10.

HTH,
Bill
This topic is locked and can not be replied to.