Link_to method calls jquery load in js.erb

I am trying to use jquery.insertafter method to dynamically insert a row into a table. I have code in 'create.js.erb' as shown below:

$("<tr><td><%= @work.id %></td><td><%= @work.user.lname %>, <%=@work.user.fname %></td><td><%= link_to(@work.project.name, @work.project, 'data-no-turbolink' => true) %></td><td><%=@work.datetimeperformed%></td><td><%=@work.hours %></td></tr>").insertAfter("tr#headerrow");

      

The code works fine if I don't use the 'link_to' method. The moment I use 'link_to' it stops working.

I'm tired of adding a warning before the "insertafter" method. Adding link_to doesn't even fire the warning.

The page seems to look great, no javascript errors. rails console log looks ok:

User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 281110143]]
Project Load (0.1ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1  [["id", 730774123]]
Rendered works/create.js.erb (3.0ms)

      

This is my controller code where I am calling create.js.erb. The .js {} format calls create.js.erb.

  def create
   @work = Work.create(work_params)

   respond_to do |format|
     if @work.save
       format.html {redirect_to @work, notice: 'Work Created'}
       format.js { render content_type: 'text/javascript'}
     else
       format.html {render 'new'}
       format.js {}
     end
   end

      

end

I got tired of these decisions from regarding turbo pumps and it didn't help either.

Rails, javascript not loading after clicking link_to

Rails 4: disable Turbolinks on a specific page

Using turbolinks in Rails link_to

Appreciate your help in this.

Thank! Mike G

+3


source to share


1 answer


Can I view the version with link_to link?

I can't say what you provided, but are you sanitizing your html before you put it in a jquery string?

What I think you are doing:

$("<%= link_to 'whatever', 'whatever' %>")
#=> $("<a href="whatever">whatever</a>")

      



What should you do:

$("<%=j link_to 'whatever' %>")
#=> $("<a href=\"whatever\">whatever</a>")

      

The "j" at the beginning is a method that makes sure it doesn't run away from the javascript string.

Without further information, this is my best guess.

+2


source







All Articles