【问题标题】:Ruby on Rails 2.3, form_tag submitted to Rails controller, JS function not calledRuby on Rails 2.3,form_tag 提交到 Rails 控制器,JS 函数未调用
【发布时间】:2014-03-07 03:33:46
【问题描述】:

当前设置:Ruby on Rails 2.3 和 Ruby 1.8.7(别问...我们正在努力)。

form_tag 帮助器在 Bootstrap 模式中用于调用 Rails 控制器中的操作。这是视图 (.haml)

- form_tag({:action => 'update_logins'},
{:method => :post, :multipart => true, :id => 'upload_form', :target => 'upload_target', :class=>'form-horizontal' })

正确调用了控制器操作,但我正在返回对 Javascript 函数的调用,尽管进行了多次尝试,但它并未调用该函数,而是呈现文本。即使是控制器中的简单渲染语句也不起作用:render :js => "alert('hello');"

我感觉这与请求标头似乎不接受 javascript 有关:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

响应头表示Javascript:Content-Type:text/javascript; charset=utf-8

并且正确收到响应:alert('hello');

但没有成功让它实际调用任何函数。

有什么想法吗?

【问题讨论】:

    标签: javascript ruby-on-rails ajax forms ruby-on-rails-2


    【解决方案1】:

    我假设您正在使用 SLIM/HAML 作为模板引擎。 检查您是否在 HTML 中生成 <form> 标记

    你可以试试替换

    - form_tag({:action => 'update_logins'},{:method => :post, :multipart => true, :id => 'upload_form', :target => 'upload_target', :class=>'form-horizontal' })
    

    = form_tag({:action => 'update_logins'},{:method => :post, :multipart => true, :id => 'upload_form', :target => 'upload_target', :class=>'form-horizontal' })
    

    这样,它将在您的 html 中创建一个<form>

    【讨论】:

    • HTML 中已经有一个<form>。当我确实将其更改为此进行尝试时,不幸的是,它破坏了一些东西,并且模态甚至不再出现:ActionView::TemplateError (form_tag outputs directly to the Haml template. Disregard its return value and use the - operator, or use capture_haml to get the value as a String.
    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 2021-07-06
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多