Hi all, I tried to use ajax in a simple eshop application, but the page.replace_html fail in strange mode. Controller code: class mycontroller < ApplicationController # Some code def login # Check user existence in db # if the user exist replace the login form with the user menu'' class mycontroller < ApplicationController # Some code def login # Check user existence in db # if the user exist replace the login form with the user menu'' if session[:user] != nil create_user_menu end end def create_user_menu @user = session[:user] render :update do |page| page.replace_html "login", :partial => ''user_menu'', :locals => { :user => @user } page.visual_effect :highlight, "login" end end end Layout code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <!-- code to build the layout --> <div id="login"> <%= render :partial => ''login'' %> </div> <!-- more code... --> </html> The partial _login.rhtml code: <%= start_form_tag({:action => ''login''}, {:name => ''form_login''}) %> <%= render :partial => ''login_form'' %> <div id="submit_link"> <%= link_to_function("Login", "document.form_login.submit()") %> </div> <%= end_form_tag %> The partial _user_menu.rhtml contain only a simple html table. When i click on "Login" link, the browser show a new page with my_controller/login action and the code generted by the js helper, instead the correct user menu in the page my_controller. Where is the error? Thanks in advance, Emiliano. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
do you have this line somewhere in your layout header ? <%= javascript_include_tag :defaults %> fo the ajax stuff you need prototype and its friends On 1 Jun., 14:22, Emiliano <e.cazz...-1Ph4/jfBtgs@public.gmane.org> wrote:> Hi all, > I tried to use ajax in a simple eshop application, but the > page.replace_html fail in strange mode. > > Controller code: > > class mycontroller < ApplicationController > # Some code > > def login > # Check user existence in db > > # if the user exist replace the login form with the user menu'' > class mycontroller < ApplicationController > # Some code > > def login > # Check user existence in db > > # if the user exist replace the login form with the user menu'' > if session[:user] != nil > create_user_menu > end > end > > def create_user_menu > @user = session[:user] > > render :update do |page| > page.replace_html "login", :partial => ''user_menu'', :locals > => { :user => @user } > page.visual_effect :highlight, "login" > end > end > end > > Layout code: > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > > <html> > <!-- code to build the layout --> > > <div id="login"> > <%= render :partial => ''login'' %> > </div> > > <!-- more code... --> > </html> > > The partial _login.rhtml code: > > <%= start_form_tag({:action => ''login''}, {:name => ''form_login''}) %> > <%= render :partial => ''login_form'' %> > <div id="submit_link"> > <%= link_to_function("Login", "document.form_login.submit()") > %> > </div> > <%= end_form_tag %> > > The partial _user_menu.rhtml contain only a simple html table. > When i click on "Login" link, the browser show a new page with > my_controller/login action and the code generted by the js helper, > instead the correct user menu in the page my_controller. > Where is the error? > > Thanks in advance, Emiliano.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You use a normal form to send the login form''s content. this launched a full request cycle and results in a complete page being loaded. so the browser expects your server to return a complete page, while your server only returns a bit of javascript and the partial, which should of course only be done when the request was an AJAX request. You have to use remote_form_for or form_remote_tag to send the form data via AJAX.See the API for Documentation. api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000529 api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000530 On 1 Jun., 14:22, Emiliano <e.cazz...-1Ph4/jfBtgs@public.gmane.org> wrote:> Hi all, > I tried to use ajax in a simple eshop application, but the > page.replace_html fail in strange mode. > > Controller code: > > class mycontroller < ApplicationController > # Some code > > def login > # Check user existence in db > > # if the user exist replace the login form with the user menu'' > class mycontroller < ApplicationController > # Some code > > def login > # Check user existence in db > > # if the user exist replace the login form with the user menu'' > if session[:user] != nil > create_user_menu > end > end > > def create_user_menu > @user = session[:user] > > render :update do |page| > page.replace_html "login", :partial => ''user_menu'', :locals > => { :user => @user } > page.visual_effect :highlight, "login" > end > end > end > > Layout code: > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" > "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > > <html> > <!-- code to build the layout --> > > <div id="login"> > <%= render :partial => ''login'' %> > </div> > > <!-- more code... --> > </html> > > The partial _login.rhtml code: > > <%= start_form_tag({:action => ''login''}, {:name => ''form_login''}) %> > <%= render :partial => ''login_form'' %> > <div id="submit_link"> > <%= link_to_function("Login", "document.form_login.submit()") > %> > </div> > <%= end_form_tag %> > > The partial _user_menu.rhtml contain only a simple html table. > When i click on "Login" link, the browser show a new page with > my_controller/login action and the code generted by the js helper, > instead the correct user menu in the page my_controller. > Where is the error? > > Thanks in advance, Emiliano.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ha scritto:> You use a normal form to send the login form''s content. this launched > a full request cycle and results in a complete page being loaded. > so the browser expects your server to return a complete page, while > your server only returns a bit of javascript and the partial, which > should of course only be done when the request was an AJAX request. > > You have to use remote_form_for or form_remote_tag to send the form > data via AJAX.See the API for Documentation. > api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000529 > api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000530 >Yes! This is the solution. Many thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---