Forum: Ruby on Rails Cancel Render after Ajax Response

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.
Chamnap (Guest)
on 2007-06-08 06:21
(Received via mailing list)
Hello,

I have one problem related to Ajax and render :partial. I develop a
small search application. When there are the results that match to the
user's search, it will render the partial views of the result search
to replace part of the document. However, when the result is not
matched, I want to display an alert box to the user back and keep the
previous result's search on the screen without erasing it. Is there a
way to cancel the render? Normally, after Ajax response, it will erase
the previous content. I want to know how to prevent this? Here is my
code:

if @result.length > 0
  render :partial => "result_list"
else
  # ......
end

Any idea? Please, help.....

Thanks
Chamnap
Cyril M. (Guest)
on 2007-06-08 11:41
(Received via mailing list)
On 6/8/07, Chamnap <removed_email_address@domain.invalid> wrote:
> the previous content. I want to know how to prevent this? Here is my
> code:
>
> if @result.length > 0
>   render :partial => "result_list"
> else
>   # ......
> end
>
> Any idea? Please, help.....
>

I think that you made this test in your view, or you can define another
partial


--
Cyril M.
javier ramirez (Guest)
on 2007-06-08 11:49
(Received via mailing list)
Hi,

> to replace part of the document. However, when the result is not
> matched, I want to display an alert box to the user back and keep the
> previous result's search on the screen without erasing it.
In your view code, in the "whatever_remote" you are using just don't use
an ":update" option. When you use update, you are telling rails to
replace the html of the given element with the response of the action
called. If the response is blank, then you get an empty element on
screen as a result.

If you are not using "update", you are telling rails not to replace
anything in your view, but to have instead the flexibility to use the
javascript "document" object for whatever purpose you want (usually for
multiple element updates). You can use rjs (or from the controller
directly a render :update) in which you can use the "page" object as a
wrapper for javascript "document".

When you have a result, you can do a page.replace_html your_div_id,
your_content
when you lack a result you can do a page.alert ('No results')

That shoudl do the trick. Regards,

javier ramírez
Titou T. (Guest)
on 2007-06-08 13:28
render :nothing => true ? ;)

Chamnap wrote:
> Hello,
>
Chris H. (Guest)
on 2007-06-08 14:18
(Received via mailing list)
try something like this:

render :update do |page|
  if search_results
    page.replace_html 'search_results_div', :partial => 'search_results'
  else
    page.call 'alert', 'No Results!'
  end
end
Chamnap (Guest)
on 2007-06-11 06:51
(Received via mailing list)
Hello,

Thanks javier ramírez and Titou T.. It works now. Great, I learned
something new from you both.

Thanks
Chamnap
This topic is locked and can not be replied to.