Ruby on rails 如何从Rails中的select标记生成中的表

Ruby on rails 如何从Rails中的select标记生成中的表,ruby-on-rails,Ruby On Rails,是否可以(如何)根据select标记更改表格 index.html.erb中的代码 <p><%= select_tag "Stocks", options_from_collection_for_select(@stocks, "id", "ticker") %></p> <table class="table"> <thead> <tr> <th>Date

是否可以(如何)根据select标记更改表格

index.html.erb中的代码

<p><%= select_tag "Stocks", options_from_collection_for_select(@stocks, "id", "ticker") %></p>
    <table class="table">
      <thead>
        <tr>
          <th>Date</th>
          <th>Market Price</th>           
        </tr>
      </thead>
      <tbody>
        <% @stock.market_prices.order(:closing_date).reverse.each do |mp|%>
        <tr> 
          <td><%=mp.closing_date %></td>
          <td><%=number_with_precision(mp.closing_price, precision: 2) %></td>
        <%end%>
      </tbody>
    </table>
index.html.erb

<script type="text/javascript">
  $(document).ready(function() {
    $('#stock').change(function() {
      var url_path;
      url_path = "/get_stock_market_price"; // Please replace this url with your original action path.
      return $.ajax({
        url: url_path,
        data: "stock_id=" + this.value,
        success: function(result) {}
      });
    });
  });
</script> 

  <p>
    <%= select_tag "Stocks", options_from_collection_for_select(@stocks, "id", "ticker"), id: 'stock' %>
  </p>

      <div id="marketdata">
        <%= render partial: "market_prices", object: @stock %>
      </div>
routes.rb

get "/get_stock_market_price" => "market_prices#get_stock_market_price"
控制器

def update_table
  @stocks = Stock.where(id => params[:id]).all
  render :partial => "update_table", :object => @stocks
end
视图 _更新_table.html.erb

<table class="table">
      <thead>
        <tr>
          <th>Date</th>
          <th>Market Price</th>           
        </tr>
      </thead>
      <tbody>
<% @stock.market_prices.order(:closing_date).reverse.each do |mp|%>
        <tr> 
          <td><%=mp.closing_date %></td>
          <td><%=number_with_precision(mp.closing_price, precision: 2) %></td>
</tr>

    <%end%>
     </tbody>
         </table>

日期
市场价格
index.htmlerb

    <div id = "tablesDiv"></div>



还可以在路由中添加/update_表的路由。rb

您可以执行以下操作

index.html.erb

<script type="text/javascript">
  $(document).ready(function() {
    $('#stock').change(function() {
      var url_path;
      url_path = "/get_stock_market_price"; // Please replace this url with your original action path.
      return $.ajax({
        url: url_path,
        data: "stock_id=" + this.value,
        success: function(result) {}
      });
    });
  });
</script>

<p>
  <%= select_tag "Stocks", options_from_collection_for_select(@stocks, "id", "ticker"), id: 'stock' %>
</p>

<div id="marketdata">
  # Assuming you have stock object in @stock, if not then change following @stock with @stocks.first
  <%= render partial: "market_prices", object: @stock %>
</div>
<table class="table">
  <thead>
    <tr>
      <th>Date</th>
      <th>Market Price</th>           
    </tr>
  </thead>
  <tbody>
    <% @stock.market_prices.order(:closing_date).reverse.each do |mp|%>
    <tr> 
      <td><%=mp.closing_date %></td>
      <td><%=number_with_precision(mp.closing_price, precision: 2) %></td>
    <%end%>
  </tbody>
</table>
def get_stock_market_price
  @stock = Stock.where(id: params[:stock_id]).first
end
$("#marketdata").html('#{ escape_javascript(render partial: "market_prices", object: @stock ) }');
get "/get_stock_market_price" => "stocks#get_stock_market_price"
get\u stock\u market\u price.js.erb

<script type="text/javascript">
  $(document).ready(function() {
    $('#stock').change(function() {
      var url_path;
      url_path = "/get_stock_market_price"; // Please replace this url with your original action path.
      return $.ajax({
        url: url_path,
        data: "stock_id=" + this.value,
        success: function(result) {}
      });
    });
  });
</script>

<p>
  <%= select_tag "Stocks", options_from_collection_for_select(@stocks, "id", "ticker"), id: 'stock' %>
</p>

<div id="marketdata">
  # Assuming you have stock object in @stock, if not then change following @stock with @stocks.first
  <%= render partial: "market_prices", object: @stock %>
</div>
<table class="table">
  <thead>
    <tr>
      <th>Date</th>
      <th>Market Price</th>           
    </tr>
  </thead>
  <tbody>
    <% @stock.market_prices.order(:closing_date).reverse.each do |mp|%>
    <tr> 
      <td><%=mp.closing_date %></td>
      <td><%=number_with_precision(mp.closing_price, precision: 2) %></td>
    <%end%>
  </tbody>
</table>
def get_stock_market_price
  @stock = Stock.where(id: params[:stock_id]).first
end
$("#marketdata").html('#{ escape_javascript(render partial: "market_prices", object: @stock ) }');
get "/get_stock_market_price" => "stocks#get_stock_market_price"
Routes.rb

<script type="text/javascript">
  $(document).ready(function() {
    $('#stock').change(function() {
      var url_path;
      url_path = "/get_stock_market_price"; // Please replace this url with your original action path.
      return $.ajax({
        url: url_path,
        data: "stock_id=" + this.value,
        success: function(result) {}
      });
    });
  });
</script>

<p>
  <%= select_tag "Stocks", options_from_collection_for_select(@stocks, "id", "ticker"), id: 'stock' %>
</p>

<div id="marketdata">
  # Assuming you have stock object in @stock, if not then change following @stock with @stocks.first
  <%= render partial: "market_prices", object: @stock %>
</div>
<table class="table">
  <thead>
    <tr>
      <th>Date</th>
      <th>Market Price</th>           
    </tr>
  </thead>
  <tbody>
    <% @stock.market_prices.order(:closing_date).reverse.each do |mp|%>
    <tr> 
      <td><%=mp.closing_date %></td>
      <td><%=number_with_precision(mp.closing_price, precision: 2) %></td>
    <%end%>
  </tbody>
</table>
def get_stock_market_price
  @stock = Stock.where(id: params[:stock_id]).first
end
$("#marketdata").html('#{ escape_javascript(render partial: "market_prices", object: @stock ) }');
get "/get_stock_market_price" => "stocks#get_stock_market_price"

即使使用jquery,您也可以在select onChange上添加Ajax函数。@AmitSharma不确定它是如何工作的。你能详细说明一下你的答案吗?我已经发布了我的答案,请检查它是否对你有帮助,如果它没有找到视图。当我在index.html.erb视图中切换到@stocks.first时,它就崩溃了。你在app/views/stocks文件夹中添加了
get_stock\u market\u price.js.erb
视图了吗?没有,但现在它呈现了“#escape_javascript(呈现部分:“market\u prices”,object:@stock)}”,但没有表。你能发布你的代码吗,我想你错过了一些帖子。我希望你能帮忙。