【问题标题】:Using RoR form_for with collection_select将 RoR form_for 与 collection_select 一起使用
【发布时间】:2016-11-05 03:31:24
【问题描述】:

在 form_for @estimate 中,我使用 collection_select 为我提供“潜在客户”表中潜在客户名称的下拉列表

    <%= form_for(@estimate) do |f| %>

    <div class="field">
      <%= f.label "Lead" %><br>
      <%= f.collection_select :lead_id, @leads, :id, :full_name, prompt: true %>
    </div>
    ...

选择标签的选项正确填充了线索。我正在使用 jQuery 的 .getJSON 方法从所选选项中检索数据

    $(document).on('change', 'select#estimate_lead_id', function(e) {
      var url = "/leads";
      var data = {
        id: $(this).val()
      };
      $.getJSON(url, data, function (data, status) {        
        if (status === 200) {
          return data
          console.log(data);
          alert("THIS IS WORKING!");        
        };
      });
     });

但是,我无法让它工作。这是我的服务器日志 - 它似乎正在访问数据库。

    Started GET "/leads?id=23" for 127.0.0.1 at 2016-07-02 23:29:29-    0500
    Processing by LeadsController#index as JSON
    Parameters: {"id"=>"23"}
    Lead Load (1.1ms)  SELECT "leads".* FROM "leads"
    Rendered leads/index.html.erb within layouts/application (10.4ms)
    User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
    Completed 200 OK in 51ms (Views: 47.6ms | ActiveRecord: 1.8ms)

我从潜在客户数据库中选择一个 id = 23 的名称,但没有任何日志记录到控制台,也没有警报。我想使用这些数据来填充估算表单中的字段。我做错了什么?

【问题讨论】:

  • 如何获取状态变量的值?
  • status 来自 http 响应头 - 200 表示一切正常

标签: javascript jquery ruby-on-rails ajax


【解决方案1】:

我之前在处理异步任务时也遇到过同样的问题。 我所做的是单独调用 onchange 函数,我在 select 标记内调用它:

看起来像这样:

<select onchange="myfunction(parameterIfneeded)"> 
....
</select>

然后在我的js函数中:

function myfunction(param) {
       ..............
       .......
     //PROCESS your AJAX here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多