Rails 3 jQuery page.insert_html and TypeError: Element.insert is not a function

I replaced Prototype with jQuery for Rails 3. Now I am trying to do the following:

#photo =render 'shared/photo' = link_to_function "Add a Photo" do |page| | page.insert_html :bottom, 'photo', :partial => 'shared/photo', :object => Photo.new | end | 

So, the generated Javascript:

 try { Element.insert("photo", { bottom: "Data\n\n" }); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.insert(\"photo\", { bottom: \"Data\\n\\n\" });'); throw e }; return false; 

In rails docs, insert_html is a Prototype helper, but I thought replacing Rails.js with https://github.com/rails/jquery-ujs will replace these helpers for JQuery Helpers. I get TypeError.ElementInsert is not a function.

Am I doing something wrong? Or will I do it myself without helpers?

+6
jquery prototypejs ruby-on-rails ruby-on-rails-3
source share
3 answers

Someone just sent this question to me, maybe I can help. I am in the jquery-ujs / jquery-rails command.

Unlike prototype-ujs for rails, jquery-ujs does not come bundled with rjs support. There is a separate stone that provides jrails functionality.

We talked about the merger of the two projects , but decided to leave them separate, as they are more flexible.

+3
source share

I have a rather rudimentary workaround. In my case, I ran page.insert_html(:top ,'content', render('form')) .

The error is caused by the generated .js.rjs code trying to run Element.insert (), which is the prototype. I don’t know enough about rails to do something very productive, but adding the following to my application. Js worked out of me:

 //Check if the function exists if (typeof Element.insert !== "function") { //If not, hook it onto the $().append method. Element.insert = function (elem, ins) { $("#" + elem).append(ins.top); }; } 

Obviously this is basic and will only work with: top, so adapt to your needs. There must be a better way ...

+2
source share

yes rails.js is a js driver that js calls back elements using the data attribute in dom. (js submit, ajax links, etc.)

what you need is an override of rail prototypes that you cannot expect from a js file if it doesn't repeat the syntax of protptype a la kdoole.

this jrails fork provides what you need and works with rails 3

however it does not work with jquery for me. I searched uptodate but found nothing.

UPDATE: I managed to make it work smoothly, my mistake ...

I realized that most people use js.erb templates with jquery, which is probably the ugliest code format i have ever seen.

0
source share

All Articles