I have the following code in my view:
Ajax Demo
<%= javascript_include_tag "prototype" %>
<%= javascript_include_tag :defaults %>
This will go away
<%= link_to_remote "Do Magic", :url => { :action => "DoMagic" },
:complete => visual_effect(:fade, "myDiv", :duration => 1.5),
:html => {:style=>"background-color: #0e0;"} %>
And this in my controller:
class DemoController < ApplicationController
def index
end
def DoMagic
render_partial false
end
end
The problem I’m having is twofold:
- First, no matter where I place my :html=>{} settings or what I put in
it, it gets ignored. The style is never applied.
- Using Firebog to watch my AJAX request I see that I keep getting an
error back, even though it works in the end. Here is the error I see
coming back in Firebug: NoMethodError in DemoController#DoMagic
- Now I’ve tried using “render_partial false”, “render(:partial=>false”,
etc. but nothing works to get rid of this error message.
Note: I know this is a trivial example and that my remote call does
nothing, I just wanted to get this working first so I could add my logic
later.
Thanks for your help.
first off, the javascript_include_tag for prototype is redundant, as
prototype is included in the defaults.
second, try this
This will go away
<%= link_to_remote "Do Magic", :url => {:action => "DoMagic"}) -%>
then in your controller:
def DoMagic
render :update do |page|
page.visual_effect :fade, ‘my_div’, :duration => 1.5
end
end
your browser will make an ajax request when the link is clicked, the
server will respond with some javascript which will be evaulated and
your div should fade out.
Chris H. wrote:
first off, the javascript_include_tag for prototype is redundant, as
prototype is included in the defaults.
second, try this
Thanks a bunch! It’s working now without the error. The only issue I
have left is trying to apply my CSS style to the link_to_remote.
link_to_remote has three parameters, last two of which is a hash.
http://rubyonrails.org/api/classes/ActionView/Helpers/PrototypeHelper.html#M000412
So do this:
<%= link_to_remote(“Do Magic”, { :url => { :action => “DoMagic” } },
{:style=>“background-color: #0e0;”}) %>
(last parameter is the html_options)
Vish