【问题标题】:Reversing HTMl table value to ruby variable将 HTMl 表值反转为 ruby​​ 变量
【发布时间】:2013-08-24 12:15:57
【问题描述】:

我正在开发 ROR 应用程序,我首先从视图中的 ruby​​ 变量生成表。变量是这样的:

h=[{"folder"=>"test3", "weight"=>"100.0", "stocks"=>[{"id"=>"stock id3", "name"=>"Indian Oil Corporation Ltd.", "weight"=>"55.8"}, {"id"=>"stock id4", "name"=>"Power Finance Corporation Ltd.", "weight"=>"44.2"}]},{"folder"=>"test", "weight"=>"100.0", "stocks"=>[{"id"=>"stock id3", "name"=>"Indian Oil Corporation Ltd.", "weight"=>"55.8"}, {"id"=>"stock id4", "name"=>"Power Finance Corporation Ltd.", "weight"=>"44.2"}]},{"folder"=>"test2", "weight"=>"100.0", "stocks"=>[{"id"=>"stock id3", "name"=>"Indian Oil Corporation Ltd.", "weight"=>"55.8"}, {"id"=>"stock id4", "name"=>"Power Finance Corporation Ltd.", "weight"=>"44.2"}]}]

现在这是生成表格的方式:

<h1 align="center"> Current Portfolio </h1>
 <table id="portfolios" width="500" border="1" align="center" height="300">
<thead class="colHeaders">
    <tr><th class="weight" bgcolor="#999999"><h4> Weight in Motif </h3></th>
        <th class="name" bgcolor="#999999"><h4> Segment &amp; Stocks</h3></th>
        <th class="price" bgcolor="#999999"><h4> Name of stock </h3></th>
  </thead>

<% h.each do |stock| %>
<tr id="titles" value="Disable Sort"> 
    <th bgcolor="#CCCCCC"> <h5>  <%= stock["weight"] %> </th>
    <th bgcolor="#CCCCCC"> <h5>   <%= stock["folder"] %> </th>
    <th bgcolor="#CCCCCC"> <h5>    </th>
  </tr>

  <% stock["stocks"].each do |details| %>
      <tr id="Stocks">
        <th>    <%= details["weight"] %> </th>
        <th>    <%= details["id"] %> </th>
        <% a=details["name"] %>
        <% a.gsub! /\s+/, '-' %>
        <% str="www.dalal-street.in/" %>
        <% str=str +a %>
        <% str=str[0..-2] %>
        <td id="<%= details["name"] %>">
            <%= link_to h(details["name"]), str %> 
            </td>


   </tr>


   <% end %>
   <% end %>

 </table>

使用 jquery,我通过拖放更改股票位置。即从文件夹 test 3 到 test 的 stock id 3。现在我需要将其更新为我的反手。有什么办法可以让我的 h 变量根据表格更新为新值。

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-3 html


    【解决方案1】:

    为了将整个表变量发送回 Rails,您需要以下内容:


    A/ 在客户端用javascript构建表对象,以便收集修改后表的当前状态。 (如果我们讨论的修改类型变得复杂,您可能会对 EmberAngularjs 等前端 js 框架感兴趣)。

    => 1. 您将表格按原样显示在类似

    的路径上
         "/table/table_id" or "/table/table_id/edit"
    

    => 2. 由用户修改

    => 3. 用户点击一个按钮或任何东西将表格“保存”到服务器

    => 4.你在用户浏览器中使用jQuery或任何框架收集修改后的表的数据


    B/ 通过 POST 请求将包含您的表的前端变量发送到后端,然后发送到 rails 端的更新方法。

    => 5. 你通过向服务器发出 POST 请求将它作为参数发送回服务器,假设是在 url

         "/table/table_id/update" 
    

    post_data 是

          {table: your_modified_table_object_as_json}
    

    C/在服务器端处理发送的数据并保存。

    => 6. 在你的 Rails 控制器中,包含渲染表和实例化变量的“显示”动作,添加一个更新方法,其中包含响应 js 发布表数据的 url 的相应路由

    => 7. 在这个更新方法中,放置解析前端 js POST 请求发送的表格所需的必要处理并保存。根据您的 rails 应用数据结构,您可能需要解析发送的数据

    例子

        def update
          @table = Table.find(params[:id])
          updated_table = JSON.parse(params[:table])
          @table.update_with_table_data(updated_table) # method to be implemented in Table model
          ... response, etc
        end
    

    编辑:附加信息

    要获取表的 id 并将数据发送回服务器,您应该使用这两种方法之一


    1) 使用常规路由:

    在这种情况下,您提供表格和用户修改它的 url 包含表格的 id。应该是这样的

        # browser window current url
        /tables/TABLEID
        or 
        /tables/TABLEID/edit
    

    这样你就可以通过在javascript中调用window.location.href来获取url

        # js
        current_url = window.location.href
    

    然后用它发回数据

        #js
        update_url = current_url.replace(/\/edit$/, '')
        update_method = 'PUT'
        jQuery.ajax( url: update_url
                     method: update_method
                     ...
    

    在 Rails 方面,您的路线将被设置为在 params 哈希中为您提供 id


    2) 其他解决方案:将 id 或直接将整个路径放在 html 属性中并从 javascript 中检索它

    例子:

    您可以将此属性添加到您的表格网址中,例如:

        <table update_path="/tables/#{TABLEID}" id='table'>
        ... your table
        </table>
    

    然后在你的 javascript 中

        update_url = jQuery('#table').attr('update_path')
        ...
    

    有关表格的Javascript端处理的更详细答案,请参阅: How to deduce changing table variable using jQuery

    【讨论】:

    • 感谢您的回答,您能告诉我如何使用 jquery 获取表 ID 并从中推断变量。我是前端新手。
    • 嘿,谢谢,但我不需要这么多东西,看这个:stackoverflow.com/questions/18420151/…,如果你能帮忙,我会想办法休息的。
    • 只有一张桌子。
    • 嗨 Deepender Singla,我已经在这篇文章 stackoverflow.com/questions/18420151/… 上回答了你关于 js 方面的问题
    • 我认为我在这里的回答实际上回答了您关于将其与 Rails 集成的问题。你能在这里和你的js问题上接受我的回答吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 2014-09-22
    • 1970-01-01
    相关资源
    最近更新 更多