ruby-on-rails - Rails - 如何通过按下按钮传输数据

标签 ruby-on-rails

我对 Rails 还很陌生,所以这对我来说很难理解。我的学校项目组的前端人员设计了这个,这样当我点击所显示产品上的“更多信息”按钮时,屏幕上会出现一个弹出框,其中包含有关该产品的更多信息。

如何让我可以访问所选产品的信息,以便将其显示在弹出框中?我已经尝试使用 button_to:action 但我显然需要一个 View 模板,但我不想要这样。我只想在用户点击“更多信息”按钮时出现的弹出框中显示所选产品的信息。

这是我正在使用的代码:

<!-- Container for the Catalog Content -->
<div class="Catalog_page">
    <!-- List all products -->
    <% @products.each do |product| %>
        <div class="product_item">
            <span class="img"><%= image_tag product.image, size: "200x100" %></span>
            <h1><%= product.name %></h1>
            <!--<%= product.description %> Saved  this just incase we needed it-->
            <%= number_to_currency(product.price) %>
            <!-- Button that creates light box -->
            <button class="lightbox" id="button">More Info</button>
        </div>


    <%end%>
    <!-- backdrop is the black background of lightbox -->
    <div class="backdrop"></div>
    <!-- the box that we need to plug information to -->
    <div class="box"><div class="close"></div>
        I DON'T KNOW HOW TO PULL THIS INFORMATION FROM DATA BASE

    </div> 

</div>

最佳答案

有两种方法可以完成您的任务:

  • 您可以呈现这些“更多信息”并将其隐藏,然后在模态中显示这些信息。
<% @products.each do |product| %>
  <div class="product_item">
    <span class="img"><%= image_tag product.image, size: "200x100" %></span>
    <h1><%= product.name %></h1>
    <!--<%= product.description %> Saved  this just incase we needed it-->
    <%= number_to_currency(product.price) %>

    <!-- Button that creates light box -->

    <!-- here is the link for each product to open its modal -->

    <a data-toggle="modal" data-target="#more-info-<%= product.id %>" class="lightbox">More Info</a>

    <!-- here is the modal content -->

    <div id="more-info-<%= product.id %>"  class="modal fade" role="dialog">
      <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">More Info</h4>
          </div>
          <div class="modal-body">
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Ok</button>
          </div>
        </div>
      </div>
    </div>

  </div>
<% end %>
  • 或者您可以通过ajax获取这些信息,然后将其显示在模态上
<% @products.each do |product| %>
    <div class="product_item">
        <span class="img"><%= image_tag product.image, size: "200x100" %></span>
        <h1><%= product.name %></h1>
        <!--<%= product.description %> Saved  this just incase we needed it-->
        <%= number_to_currency(product.price) %>
        <!-- Button that creates light box -->
        <button class="lightbox fetch-info" data-product-id="<%= product.id %>">More Info</button>
    </div>
<%end%>
<!-- backdrop is the black background of lightbox -->
<div class="backdrop"></div>
<!-- the box that we need to plug information to -->
<div class="box"><div class="close"></div>
   <div id="fetch-result"></div>
</div>

然后,创建一个JS来获取数据:

$(".fetch-info").click(function() {
  var $input = $(this);
  $.ajax({
    type: "GET",
    url: "/product_info",
    data: {
      id: $input.data("product-id")
    },
    success: function(response) {
      $("#fetch-result").html(response);
    }
  });
});

然后,创建一个操作来提供产品信息:

def product_info
  @product = Product.find(params[:id])
  render layout: false
end

并使用您的自定义产品信息创建您的 View ...

就是这样!

提示:如果您选择第二个选项,您可以通过以 JSON 形式调用操作来改进(仅获取产品信息,然后在成功回调中添加 html)

关于ruby-on-rails - Rails - 如何通过按下按钮传输数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290138/

相关文章:

ruby-on-rails - Rails 更新三向连接表

mysql - Rails 中的多个数据库连接

ruby-on-rails - 为缺少模板提供 404 而不是 500 错误

ruby-on-rails - 错误 : column "increment_by" does not exist - Postgres 10 and Rails 5

javascript - 在 ember.js 中保存对象时出错 : 'Object X has no method ' save''

ruby-on-rails - 语法错误 : unexpected $end when using if/else if?

ruby-on-rails - 仅当可搜索字段发生变化时才使用太阳黑子索引

ruby-on-rails - 基于 URL 参数的 Rails 设置布局

javascript - Rails 应用程序仅在 Chrome 上正确显示。对于 Firefox 和 Safari,所有样式加载不正确

ruby-on-rails - form_tag 可以与 Simple_form 一起使用吗?