【问题标题】:Ruby on Rails: Sending javascript data to ruby controllerRuby on Rails:将 javascript 数据发送到 ruby​​ 控制器
【发布时间】:2012-06-25 22:07:17
【问题描述】:

我想发送一个 javascript 数组以由我的控制器中的方法处理。 我认为我这样做是错误的。我是一个完整的 RoR、jquery 和 ajax noobie。这就是我所拥有的。请给我一些指导:

<div id="dataTable" class="dataTable" style="width: 680px;height: 300px; overflow: scroll"></div>
<button>Save!</button>
<script>
    var first = true;
    var totalChanges = new Array();
    $("#dataTable").handsontable({
         //...some code that generates appropriate array totalChanges
    });
    var data = //..some code
    $("#dataTable").handsontable("loadData", data);
    $(function() {
            $( "button").button();
            $( "button" ).click(function() { 
                    alert("clicked"); 
                    $.ajax({
                            type: "POST",
                            url: "save",
                            data: JSON.stringify(totalChanges),
                            success: function() { alert("Success!"); }
                    });
            });
    });

</script>

我收到此错误:

POST http://10.10.136.244:6500/qtl_table/save 500 (Internal Server Error) 

Started POST "/qtl_table/save" for 10.64.229.59 at Mon Jun 25 16:58:46 -0500 2012
  Processing by QtlTableController#save as 
  Parameters: {"125,0,\"\",\"Ph upt 1-2\""=>{","=>{"125,1,\"\",\"DOR364\""=>{","=>{"125,2,\"\",\"G19833\""=>nil}}}}}
LOGGER WORKS
Completed 500 Internal Server Error in 81ms

ActionView::MissingTemplate (Missing template qtl_table/save with {:formats=>[:html], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder], :locale=>[:en, :en]} in view paths "/usr/home/benjamin/phavubase/qtl/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/declarative_authorization-0.5.5/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/devise_cas_authenticatable-1.1.1/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/devise-1.2.1/app/views", "/usr/home/benjamin/phavubase/qtl/ruby/1.8/gems/kaminari-0.12.4/app/views"):
  app/controllers/qtl_table_controller.rb:18:in `data'
  app/controllers/qtl_table_controller.rb:25:in `save'

Rendered ruby/1.8/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.8ms)

编辑: app/controllers/qtl_table_controller.rb

...
  def save
    logger.debug "\nLOOK! WE SAVED! #{params[data]}\n"
    render "index"
  end
...

我添加了 render :layout => false 但我仍然收到缺少模板的错误。另外,有人建议我刚开始向控制器添加逻辑,但数据参数看起来很时髦。它应该是我变成字符串的数组数组。你们能不能再给我一些帮助?

【问题讨论】:

  • 你能发布你的控制器代码吗?它可能正在保存,然后只是尝试渲染模板。
  • 我的控制器中现在没有任何逻辑。

标签: ruby-on-rails ajax


【解决方案1】:

您应该在app/views/qtl_table/save.html.erb 中创建一个模板,或者在您的控制器中渲染一些东西。如果控制器动作中没有呈现任何内容,Rails 会尝试显示默认模板,而您没有。

【讨论】:

  • 我在我的控制器中渲染了“索引”(这是应用程序当前所在的页面)。我希望在单击保存按钮后重新加载页面
【解决方案2】:

同意Riacheche 的回答。您需要向控制器添加一些逻辑才能正确响应。

查看您的控制台输出,您可以在参数哈希中看到 json 数据。您可以像这样简单地访问控制器中的值:

params["125,0,\"\",\"Ph upt 1-2\""]
params["125,0,\"\",\"Ph upt 1-2\""][","]

您可以将其映射到您的 activerecord 模型并保存。

【讨论】:

  • 我的参数似乎没有意义,它应该是一个数组数组。我做错了吗
【解决方案3】:

由于您希望它在 AJAX 中响应,您可以通过添加来告诉 Rails 不要渲染任何内容

render :layout => false

到控制器动作

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多