【问题标题】:Rails + AJAX: Load modal with ActiveRecord ObjectRails + AJAX:使用 ActiveRecord 对象加载模式
【发布时间】:2017-02-24 19:23:58
【问题描述】:

我有一个Product 模型我希望我的用户能够单击视图中的链接,这将导致模式显示特定于给定product 的数据。我将产品id 存储在链接data-attribute 中。

# Modal
<div class="modal" id="product-modal">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title text-center"><strong>Nutrition Facts:</strong></h4>
      </div>
            <div class="modal-body"></div>
        </div>
    </div>
</div>

然后我就有了用户会点击的链接:

# They click the label containing the data-attribute
<% @products.each do |prod| %>
  <%= f.label prod.name, data: {id: prod.id}, class: "product-info" %>
<% end %>

这调用了一个ajax函数:

$(".product-info").click(function() {
    url = "/products/" + $(this).data("id")
    $.get(url, function(data) {
       console.log(data) # nothing shows here
    }, 'script')
})

...最后我的products/show.js 视图如下:

$(".modal-body").html(<%= escape_javascript(render :partial => '/food_orders/nutrition', :locals => {prod: @product}) %>)
$("#product-modal").fadeIn();
console.log("Is this working?"); # I never see this in the console

在我的开发人员工具中,对 ajax 请求的响应包含 show.js,但从未向用户显示。

【问题讨论】:

    标签: javascript jquery ajax ruby-on-rails-4 bootstrap-modal


    【解决方案1】:

    整个问题是由于我的 erb 不在引号中。

    我变了:

    $(".modal-body").html(<%= escape_javascript(render :partial => '/food_orders/nutrition', :locals => {prod: @product}) %>)
    

    到:

    $(".modal-body").html("<%= escape_javascript(render :partial => '/food_orders/nutrition', :locals => {prod: @product}) %>")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      相关资源
      最近更新 更多