form_remote_for不执行RJS,只显示JS

时间:2009-12-21 06:04:59

标签: ruby-on-rails ajax prototypejs rjs

我在rails 2.3.3上使用prototype / rjs,在ruby 1.86上使用我的form_remote_for到我的“create”控制器只显示create.js.rjs而不是执行它。奇怪的是,所有其他动作+ rjs的工作正常,只是“创建”动作不起作用任何帮助将不胜感激。提前谢谢!

更新:澄清一下,它应该有效:_form submit - > POST - >创建方法 - > js - >加载不同的部分。相反,它正在工作:_form submit - > POST - >创建方法 - >输出js作为文本

_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

输出(显示为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 }

更新

_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

2 个答案:

答案 0 :(得分:1)

通常它意味着某些东西在部分中被打破。我之前发生过这种情况。你能展示部分代码吗?

您也可以通过使用

包装rj来使其工作
page << ' '

检查以确保您的任何视图代码中都没有更新参数。更新参数会搞砸上下文。有关详细信息,请参阅此post

答案 1 :(得分:0)

我遇到了这个问题。我通过确保&lt;%= javascript_include_tag:defaults%&gt;来解决了这个问题。在我的所有javascript_include标记中都列出了最后一个。试试这个。