Topic: form_remote_tag and redirect

I got search options builded with with form_remote_tag. So thx to AJAX i see result and then i can click one of items to see more. But if there is only one item in result i want to redirect to it automate. If i use redirect_to :action => 'show' , :id => id it will redirect but the page will apear in div that i update(not good).
How to change it?

Last edited by wojtek (2007-08-07 09:21:17)

Re: form_remote_tag and redirect

Is your page an .rjs? If it is, you can use

page.redirect :action => "whatever"

Re: form_remote_tag and redirect

nope i dint use rjs, i try to change it but it say 'error no search.rhtml'

<%=  form_remote_tag (:url => { :controller => '../examples',:action => 'search'}, :update => :result, :loading => "Element.show('spinner')", :complete => "Element.hide('spinner')")  %>

in controller examples.rb in action search i search items
and in search.rhtml
<%if @items==nil or @items.empty? %>
    no result!!!
<% else %>
<%= render :partial => 'list' %>
<% end %>

and i want somewhere one more condition ,if @items==1 redirect to show this item

Re: form_remote_tag and redirect

I think putting redirect in the controller's action should solve the problem.

class Example_Controller
  def search
    @items = get_items()
    if(@items.length() == 1)
      redirect_to(result_page_url)
    end   
  end
end

Re: form_remote_tag and redirect

nope
i already told that it wont redirect, the redirect page just appear in the div

Re: form_remote_tag and redirect

Try using inline RJS:

render :update => do |page| ... end

Full code may be seen like

<%if @items==nil or @items.empty? %>
    no result!!!
<% elsif @items.size == 1 %>
    <%= render :update => do |page| page.redirect ...path to single item... end %>
<% else %>
    <%= render :partial => 'list' %>
<% end %>

Re: form_remote_tag and redirect

You use the :update action in your form_remote_tag definition. This makes Prototype expect HTML to be returned. Now if you return a HTML header with a redirect in it (which is what redirect_to does), i think Prototype will follow that redirect and get the HTML from there, instead of redirecing the page.
So:
1) remove the :udpate option from the form_remote_tag. Now Prototype expects Javascript to be returned.

<%=  form_remote_tag (:url => { :controller => '../examples',:action => 'search'}, 
                      :loading => "Element.show('spinner')",
                      :complete => "Element.hide('spinner')")  %>

2) use RJS in the controller. It will send JavaScript to the client, and prototype will interpret it and do the wanted changes (replace a div'S content with HTML, redirect, etc.)
def search
#... some code ...

if @items==nil or @items.empty?
  #will replace the content of the tag with id "result" with the given string.
  render :update do |page|
    page[:result].replace_html "no result!!!"
  end
elseif @items.size = 1
  # will send back Javascript that does a client-side redirect to the desired URL
  render :update do |page|
    url = url_for{:action => 'show' , :id => @items[0].id}
    page << "window.location = #{url}"
  end
else
  # will render the list partial into the tag with id "result" and replace the content.
  render :update do |page|
    page[:result].replace_html render :partial => 'list'
  end
end

end


Those if statements do not belong into the view if you use AJAX.
Code if for demonstration only of course, and probably not working out of the box like this for you.

Last edited by Duplex (2007-08-28 10:24:24)