Search code examples
ruby-on-railsajaxjavascriptrjs

RJS returns plain javascript without JS tags


Here is code on view in FeesController "show" action template:

<div id="payers_controls">
      <%= link_to_remote('New payer',
                               :update => "payers_controls", 
                               :url => new_payer_url) 
      %>
</div>

Here is new.rjs - belongs to PayersController "new" action

page.replace_html "payers_controls", :partial => "form", :object => @payer

Here is "new" action in PayersController

  def new
    @payer = Payer.new(params[:payer])
  end

Here is "form" partial for "new" action in PayersController

   <%= "Damn text to see if this damn partial is rendered properly" %>

And then I click "New payer" link, it renders me plain JS inside "payers_controls" DIV:

try { Element.update("payers_controls", "Damn text to see if this damn partial is rendered properly"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"payers_controls\", \"Damn text to see if this damn partial is rendered properly\");'); throw e }

And why the hell this damn response is not enclosed in SCRIPT tags? Do I need to create another hidden placeholder with SCRIPT tags in page for JS responses?

P.S. Please understand my fury, because I'm new at AJAX on Rails, I want to be pedant programmer and, hell, I want my code to look nice and clean.


Solution

  • The problem is that you are trying to update the content twice using JS.

    I would remove the :update parameter from your link_to_remote tag, or remove the RJS template.

    Basically Rails is updating the page once using the RJS Template, then it is updating it again using the :update callback from the link_to_remote. You don't need both. Since its better to keep as little as code as possible in your views, I would remove it there and keep the RJS template.

    Hope this helps. Anything else, just let me know.

    Kent