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! -- 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.
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 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.
> > 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 > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgWell, 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. -- 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.
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 >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > Well, there''s some code inside the methodInside *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 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.
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. -- 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.
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 marnen-txHYewZdXVOEb1ujJ5R0Fw@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.
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. -- 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.
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 textAgain: you don''t need js.erb. Please follow my suggestions in http://www.ruby-forum.com/topic/215092#933470 . 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.
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 -- 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.