创建动态表:Attributes = Columns,Nested Attributes = Rows [英] Create a Dynamic Table: Attributes = Columns, Nested Attributes = Rows

查看:214
本文介绍了创建动态表:Attributes = Columns,Nested Attributes = Rows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行&专栏,宝贝。行&列。

Rows & columns, baby. Rows & columns.

在一个表格中:

当用户添加新名称时:名称& :metric我希望它创建一个新列。

When a User adds a new :name & :metric I want it to create a new column.

当用户添加新的:result_value& :date_value我希望它创建一个新行。

When a User adds a new :result_value & :date_value I want it to create a new row.

这是我的表目前的样子:

This is what my table currently looks like:

对于我的生活,我无法想象为什么看似如此基本的东西如此困难。我不知道我是否想在数据库,模型,帮助器或索引视图中创建行和列和/或我是否应该使用其他语言或gem来帮助我。任何方向都会非常感激,因为十几个谷歌搜索还没有为我完成。

For the life of me I can't figure out why something that seems so elementary is so difficult. I don't know if I'm suppose to create the rows and columns in the database, model, helper or index view and/or if I should be using some other language or gem to help me. Any direction would be greatly appreciated because a dozen google searches hasn't done it for me.

我是否使用 add_column 在数据库中还是什么?

Do I use add_column in the database or something?

class CreateQuantifieds < ActiveRecord::Migration
  def change
    create_table :quantifieds do |t|
      t.string :categories
      t.string :name
      t.string :metric

      t.timestamps null: false
    end
  end
end

或者我是否通过模型添加列?

Or do I add columns via the model?

class Quantified < ActiveRecord::Base
    belongs_to :user
    has_many :results #correct
    accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true #correct
    scope :averaged,  -> { where(categories: 'averaged') }
    scope :instance,  -> { where(categories: 'instance') }

    CATEGORIES = ['averaged', 'instance']

end

class Result < ActiveRecord::Base
    belongs_to :user
  belongs_to :quantified
end

或者我一直在玩索引视图我已经尝试了各种各样的标签和html标签,但我永远无法让代码看起来像上面那样,这就是为什么我认为答案可能更深层次。

Or do I keep playing around with the index view I've tried all sorts of 's and html tags, but I could never get the code to look like above, which is why I think the answer may lie deeper.

<!-- Default bootstrap panel contents -->

<div id="values" class="panel panel-default">

  <div class="panel-heading"><h4><b>AVERAGE</b></h4></div>

<table>
  <% @averaged_quantifieds.each do |averaged| %>
    <% if averaged.user == current_user %>     
      <th><%= averaged.name %> (<%= averaged.metric %>)</th>
      <td><% averaged.results.each do |result| %>
        <tr><td><%= result.date_value.strftime("%m-%Y") %></td>
        <td><%= result.result_value %></td></tr>
    <% end %></td>
  <% end %>
  <% end %>
</table>
</div>

<br>
<br>
<br>
<br>

<!-- Default bootstrap panel contents -->
<div id="values" class="panel panel-default">

  <div class="panel-heading"><h4><b>INSTANCE</b></h4></div>

<table>
  <% @instance_quantifieds.each do |instance| %>
    <% if instance.user == current_user %>     
      <th><%= instance.name %> (<%= instance.metric %>)</th>
      <td><% instance.results.each do |instance| %>
        <tr><td><%= result.date_value.strftime("%m-%Y") %></td>
        <td><%= result.result_value %></td></tr>
    <% end %></td>
  <% end %>
  <% end %>
</table>
</div>

  <div class="values-button">
  <%= link_to new_quantified_path, class: 'btn'  do %>
  <b><span class="glyphicon glyphicon-plus"</span></b>
  <% end %>
  </div>


推荐答案

我想你可能对数据库的方式有误解应该使用。

I think you may have a misunderstanding of how the database should be used.

我认为你不希望实际的数据库表是动态的。相反,您应该设置两个表。一个用户,一个用于度量。然后用户可以添加新指标。

I don't think you would ever want the actual database tables to be dynamic. Instead, you should set up two tables. One for the User, and one for the Metrics. Then a user can add new metrics.

# == Schema Information
# Table name: users
# id :integer
Class User < ActiveRecord::Base
  has_many :metrics
end

# == Schema Information
# Table name: metrics
# id :integer
# type :string
# result :string
# user_id :integer
Class Metric < ActiveRecord::Base
  belongs_to :user
end

这样,你可以根据需要创建多种类型的指标。您可以通过:user_id字段确定指标所属的人。

This way, you can create as many types of metrics as you want. And you can determine who the metric belongs to by the :user_id field.

从头到脚阅读此内容应该有所帮助: http://guides.rubyonrails.org/association_basics.html

Reading this from head to toe should help: http://guides.rubyonrails.org/association_basics.html

这篇关于创建动态表:Attributes = Columns,Nested Attributes = Rows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆