hey guys, I am having an issue with AJAX on rails 3. I don''t know if it''s a bug or something but definitely weird. Basically i have {model,controller,view} that work perfectly over http. I have an index action in my "articles" controller that searches for a search query supplied from the form on the index view. the weird behavior: 1. using firebug, i can see that the AJAX request is sent to rails and the response i get is the content of the file index.js.erb! and obviously nothing changes on the page.. rails console log shows that the template index.js.erb was rendered successfully and all is OK (200). my application layout has: [code] <head> <title>Blog</title> <%= stylesheet_link_tag :all %> <%= javascript_include_tag :defaults %> <%= csrf_meta_tag %> </head> [/code] i.e. all prototype is loaded my index action: [code] def index if params[:search] @articles = Article.search params[:search] else @articles = Article.all end respond_to do |format| format.html # index.html.erb format.xml { render :xml => @articles } format.js end end [/code] my index.erb.html [code] <%= form_tag articles_path, :method => :get, :remote => true do %> <%= text_field_tag :search %> <%= submit_tag :search %> <% end %> <div id="listing"> <%= render :partial => ''list'' , :locals => { :articles => @articles } %> </div> <%= link_to ''New Article'', new_article_path %> [/code] my index.js.erb has: [code] page.replace_html ''listing'', partial => ''list'', :locals => { :articles => @articles } [/code] the content of the js.erb template is what is returned in the response! why is this happening??? 2. the second weird behavior: if I change the JS template file from .js.erb to .rjs, rails renders the http template file!!! even though it received and AJAX request (as evident in firebug),, that''s really weird.. Please shed some light on why all of this is happening and how do i get it my partial to replace the ''listing'' div html. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 30, 10:18 am, Naif Dalbahi <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> my index.js.erb has: > [code] > page.replace_html ''listing'', partial => ''list'', :locals => { :articles > => @articles } > [/code] > > the content of the js.erb template is what is returned in the response! > why is this happening???Because the above is not an erb template - or rather it contains no erb tags (<%= and so on). I believe the ''correct'' name for rjs templates these days is js.rjs. Fred> > 2. the second weird behavior: > if I change the JS template file from .js.erb to .rjs, rails renders the > http template file!!! even though it received and AJAX request (as > evident in firebug),, that''s really weird.. > > Please shed some light on why all of this is happening and how do i get > it my partial to replace the ''listing'' div html. > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Sep 30, 10:18�am, Naif Dalbahi <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> my index.js.erb has: >> [code] >> page.replace_html ''listing'', partial => ''list'', :locals => { :articles >> => @articles } >> [/code] >> >> the content of the js.erb template is what is returned in the response! >> why is this happening??? > > Because the above is not an erb template - or rather it contains no > erb tags (<%= and so on). > I believe the ''correct'' name for rjs templates these days is js.rjs. > > FredThank you very, very much Fred. You saved my day man! I am so angry that the extension change was never mentioned in rails 3 documentation site... how are people supposed to know about that if the development team doesn''t announce that?! Thank you again Fred,, very much appreciated. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sep 30, 11:26 am, Naif Dalbahi <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote:> I am so angry that the extension change was never mentioned in rails 3 > documentation site... how are people supposed to know about that if the > development team doesn''t announce that?!I thought the ''new'' extensions were added in 2.0 and the old ones had been vaguely deprecated ever since. Fred> > Thank you again Fred,, very much appreciated. > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Naif Dalbahi wrote:> I am so angry that the extension change was never mentioned in rails 3 > documentation site... how are people supposed to know about that if the > development team doesn''t announce that?!This isn''t a new change at all, and especially not specific to Rails 3. The code you had inside of your js.erb template was RJS code, which will really limit you in places. The appropriate javascript equivalent inside of your js.erb template would be: $(''#listing'') .replace("<%= escape_javascript(render :partial => ''list'', :locals => { :articles => @articles }) %>"); Note that is jQuery and not Prototype, but it applies regardless. Embrace the Javascript! -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Parker Selbert wrote:> Naif Dalbahi wrote: >> I am so angry that the extension change was never mentioned in rails 3 >> documentation site... how are people supposed to know about that if the >> development team doesn''t announce that?! > > This isn''t a new change at all, and especially not specific to Rails 3. > The code you had inside of your js.erb template was RJS code, which will > really limit you in places. The appropriate javascript equivalent inside > of your js.erb template would be: > > $(''#listing'') > .replace("<%= escape_javascript(render :partial => ''list'', :locals => > { :articles => @articles }) %>"); > > > Note that is jQuery and not Prototype, but it applies regardless. > Embrace the Javascript!Thank you Fred & Parker... You guys are awesome!.. I guess I haven''t kept track of rails over the past years.... -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Sep-30 15:39 UTC
Re: Rails3: ajax responds with js template text!
Parker Selbert wrote:> Naif Dalbahi wrote: >> I am so angry that the extension change was never mentioned in rails 3 >> documentation site... how are people supposed to know about that if the >> development team doesn''t announce that?! > > This isn''t a new change at all, and especially not specific to Rails 3. > The code you had inside of your js.erb template was RJS code, which will > really limit you in places. The appropriate javascript equivalent inside > of your js.erb template would be: > > $(''#listing'') > .replace("<%= escape_javascript(render :partial => ''list'', :locals => > { :articles => @articles }) %>"); > > > Note that is jQuery and not Prototype, but it applies regardless. > Embrace the Javascript!Better yet, use static JavaScript files and banish js.erb from your vocabulary. (RJS may occasionally be useful, but in general JS shouldn''t be dynamically generated IMHO.) Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.