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!
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.
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.
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.
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.
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?
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.
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