views:

355

answers:

1

Hi,

I'm using prototype/rjs on rails 2.3.3 and ruby 1.86 and my form_remote_for to my "create" controller just displays the create.js.rjs instead of executing it. The strange this is that all the other action+rjs's work fine, just the "create" action isn't working Any help would be appreciated. Thanks in advance!

UPDATE: To clarify, it should work: _form submit -> POST -> create method -> js -> load different partial. Instead it is working: _form submit -> POST -> create method -> outputting js as text

_form.html.haml:

- form_remote_for(@network) do |f|
  = f.error_messages
  %p><
    = f.label :name
    = f.text_field :name
  %p><
    = f.label :image_url
    = f.text_field :image_url
  %p><
    = f.label :url
    = f.text_field :url
  %p><
    = f.submit "Create"

create.js.rjs:

page.insert_html(:bottom, :networks, :partial => 'networks/network', :object => @network);

networks_controller:

def create
  @network = Network.new(params[:network])

  respond_to do |format|
    if @network.save
      flash[:notice] = 'Network was successfully created.'
      format.js #also doesn't work with { render :layout => false }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @network.errors, :status => :unprocessable_entity }
      format.js
    end
  end
end

output (it displays as html):

try {
Element.insert("networks", { bottom: "<div class='network'>\n  <img alt=\"Df\" src=\"/images/df\" />\n  <a href=\"d\">wer</a>\n  <a href=\"/networks/39\">Show</a>\n  <a href=\"/networks/39/edit\">Edit</a>\n  <a href=\"/networks/39\" onclick=\"if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'dfleb/EzulRpXnZekVfbhAkX3G9jHmhnWTBLSk7iRSE='); f.appendChild(s);f.submit(); };return false;\">Destroy</a>\n</div>\n" });
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.insert(\"networks\", { bottom: \"<div class=\'network\'>\\n  <img alt=\\\"Df\\\" src=\\\"/images/df\\\" />\\n  <a href=\\\"d\\\">wer</a>\\n  <a href=\\\"/networks/39\\\">Show</a>\\n  <a href=\\\"/networks/39/edit\\\">Edit</a>\\n  <a href=\\\"/networks/39\\\" onclick=\\\"if (confirm(\'Are you sure?\')) { var f = document.createElement(\'form\'); f.style.display = \'none\'; this.parentNode.appendChild(f); f.method = \'POST\'; f.action = this.href;var m = document.createElement(\'input\'); m.setAttribute(\'type\', \'hidden\'); m.setAttribute(\'name\', \'_method\'); m.setAttribute(\'value\', \'delete\'); f.appendChild(m);var s = document.createElement(\'input\'); s.setAttribute(\'type\', \'hidden\'); s.setAttribute(\'name\', \'authenticity_token\'); s.setAttribute(\'value\', \'dfleb/EzulRpXnZekVfbhAkX3G9jHmhnWTBLSk7iRSE=\'); f.appendChild(s);f.submit(); };return false;\\\">Destroy</a>\\n</div>\\n\" });'); throw e }

Update:

_networks.html.haml:

%div{:id=> 'network#{network.id}', :name => 'network'}
  = image_tag network.image_url
  = link_to network.name, network.url
  - if logged_in? && self.current_user.login = "admin"
    = link_to 'Show', network
    = link_to 'Edit', network_path(network), :method => :put, :class => "put"
    = link_to 'Destroy', network_path(network), :method => :delete, :class => "delete"
  %br
+1  A: 

Usually it means something is broken in the partial. I have had this happen before. Could you show the partial code?

You might also get it to work by wrapping your rjs with

page << ' '

Check to make sure you don't have an update parameter in any of your view code. The update parameter screws up the context. See this post for more details.

coreypurcell
Corey, thanks for the reply. There is no partial - it's a POST via create so it just redirects on the html
I am referring to this line:page.insert_html(:bottom, :networks, :partial => 'networks/network',What is in the networks/network partial?
coreypurcell
I've added the networks/network partial. Thanks for the link for the explanation - makes a lot of sense; however, wrapping the it in "page << ' '" didn't solve the issue. One other factor that I didn't think would affect this problem was that I'm using an separate index controller/view which has a networks/network partial, and loading the networks/form partial via rjs on a "new network" link, and hoping to refresh the div by re-loading the networks/network partial. There's got to be a better way to do this, but it would be great to understand this issue for the future. Any thoughts?
Nothing in your code is jumping out at me as being wrong. I did notice that the html generated in the AJAX response is not the same as I would expect according to your partial. Specifically the div tag doesn't have the correct attributes in the generated js. I'd start the debugging there, and look at what Firebug gives you in the AJAX response.
coreypurcell
As for your other question, I wouldn't use a server side call to just show the form on the new link. I would have the html already on the page and just use some client side code to show the hidden form. That will save you an http request. Let me know if you need more detail on how to do that.
coreypurcell
Update parameter in a `link_to_remote` call was the cause of this for me.
nuclearsandwich