views:

382

answers:

3

After having Googled and hurting my brain for hours on this, I finally decided to post this question. Here's the code...

view1.html.erb
-------------- 
<%=link_to_remote_redbox "Link", :url => {:action => :action1, :id => @some.id} 

some_controller.rb
------------------
def action1
  render :layout => false
end

def action2
  do some processing
end


action1.html.erb
--------------------
<form onsubmit="new Ajax.Request('/some_controller/action2', {asynchronous:true, evalScripts:true, onComplete:function(request){RedBox.close(); return false;}, parameters:Form.serialize(this)}); return false;}" method="post" action="/some_controller/action2">
<input type=text name='username'>
<input type='submit' value='submit'> 
</form>  

action2.rjs
-----------
page.replace_html("some_div", (render(:partial => "some_partial")))

with that code in place when action2.rjs kicks in it should display the html page instead I am getting this

Element.update("some_div", "<style type=\"text/css\">\n\n..............

As suggested on other posts I read, they say its caused because of the ":update => some_div" in the link_to_remote_redbox function but clearly my code doesn't have that.

Help is always appreciated. Many Thanks

A: 

Make sure the layout includes the default javascript files. In other words:

<%= javascript_include_tag :defaults %>

graza
Hi. Yes, the layout does load all the default Javascripts
Anand
@graza, this should be a comment if you're just making suggestions for him to try something. If you don't see a bug in his code paste, try soliciting more details first instead of taking a shot in the dark.
macek
A: 

Try following:-

def action2
  render :update do|page|
    page.replace_html, 'some_div', :partial => 'some_partial'
  end
end

OR

<form onsubmit="new Ajax.Request('', {asynchronous:true, evalScripts:true, onComplete:function(request){RedBox.close(); return false;}, parameters:Form.serialize(this)}); return false;}" method="post" action="/some_controller/action2">
Salil
A: 

Salil's method would work if you want to call return an ajax call from your browser.. but in case you want to call more complex javascript (which could be messy in your controller) you can do this instead:

def action2
  ...some code
  respond_to do |format|
    format.js
  end
end

This will explicitly tell rails to return javascript rather than html. This is also useful if you use jQuery rather than RJS.

Cheers!

Dustin M.