Executing js.erb file with :format =>'js'

Hi, I’m trying to get my javascript to execute after clicking a link on
my page using :format => “js” here’s my link:

<%= link_to image_tag("/images/icons/user_add.png"),
add_contact_path(:profile_id => profile.id, :url => request.url, :format
=> “js”), :title => “Add person to your contacts”%>

in my controller i have format.js

here’s my add_contact.js.erb:

$("#contact_icon_<%=params[:profile_id]%>").html(“Contact Added”)

Right now it just prints that code on the screen instead of executing
it. I’ve done this before with a form where I submitted with AJAX but
never with just a straight link before. Any thoughts? Thanks!

The Ultimation wrote:

Hi, I’m trying to get my javascript to execute after clicking a link on
my page using :format => “js” here’s my link:

<%= link_to image_tag(“/images/icons/user_add.png”),
add_contact_path(:profile_id => profile.id, :url => request.url, :format
=> “js”), :title => “Add person to your contacts”%>

in my controller i have format.js

here’s my add_contact.js.erb:

$(“#contact_icon_<%=params[:profile_id]%>”).html(“Contact Added”)

Right now it just prints that code on the screen instead of executing
it. I’ve done this before with a form where I submitted with AJAX but
never with just a straight link before. Any thoughts? Thanks!

Generating JavaScript dynamically with ERb is almost never a good thing
in my experience. Make your JavaScript static by getting the profile_id
from the DOM or a URL parameter.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Generating JavaScript dynamically with ERb is almost never a good thing
in my experience. Make your JavaScript static by getting the profile_id
from the DOM or a URL parameter.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Well, there’s some code inside the method that needs to execute first,
then change the element on the screen with javascript. using js.erb is
the only way I know to do both of those things. I can change the element
on the screen with a click function but I don’t know how I’d get the
method’s code to execute as well.

The Ultimation wrote:

Generating JavaScript dynamically with ERb is almost never a good thing
in my experience. Make your JavaScript static by getting the profile_id
from the DOM or a URL parameter.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Well, there’s some code inside the method

Inside what method? Where? Is this a Ruby method or a JavaScript
method?

that needs to execute first,
then change the element on the screen with javascript.

That sounds like a very smelly design. Can you explain a bit more about
what you’re trying to do?

using js.erb is
the only way I know to do both of those things.

…which probably indicates a design problem.

I can change the element
on the screen with a click function but I don’t know how I’d get the
method’s code to execute as well.

Again, where is this method? That answer may lead to some suggestions?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

def add_contact
@contact = Contact.new(:user_id => current_user.id, :profile_id =>
params[:profile_id])
if @contact.save
flash[:notice] = “Successfully added contact.”
redirect_to params[:url]
else
flash[:error] = “Could not add contact.”
redirect_to params[:url]
end
end

That’s the method I need to execute.

Yeah, that’s what I need. The problem here, as stated in the original
post, is the javascript code in my add_contact.js.erb is being displayed
as text and not executed when it gets routed through format.js. Just
wondering how to make that code execute. I’m trying now to do it through
jquery instead of the js.erb, but I know that way is possible.

The Ultimation wrote:

def add_contact
@contact = Contact.new(:user_id => current_user.id, :profile_id =>
params[:profile_id])
if @contact.save
flash[:notice] = “Successfully added contact.”
redirect_to params[:url]
else
flash[:error] = “Could not add contact.”
redirect_to params[:url]
end
end

That’s the method I need to execute.

And you need to do that, then run some JS? If so, then your best bet is
to make an Ajax request to add_contact, with a JS callback for when the
request completes. None of that needs dynamic JavaScript. So what’s
the actual problem here?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

The Ultimation wrote:

Yeah, that’s what I need.

What’s what you need? Please quote when replying, or it will be
impossible to follow the discussion.

The problem here, as stated in the original
post, is the javascript code in my add_contact.js.erb is being displayed
as text

Again: you don’t need js.erb. Please follow my suggestions in
Executing js.erb file with :format =>'js' - Rails - Ruby-Forum .

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

yeah I’m now trying to do something more along the lines of what you
suggested with jquery’s $.ajax function. Almost there for the most part.
Thanks for your suggestions