开发者

form_remote_for not executing RJS, just displays JS

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


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.


I had this exact problem. I solved it by ensuring that <%= javascript_include_tag :defaults %> was listed LAST among all my javascript_include tags. Try this out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜