生成脚手架时,Ruby on Rails默认为每个属性创建一个新列,但如何将其中一些属性更改为行?
我是Rails的初学者,一般都是编码。过去几个月我一直在广泛使用Rails,但我还有一些方法可以解决问题的新手,所以请用温柔的双手和初学者的心思来处理这个问题。
我甚至不需要你来解决这个问题,但只是给我一些明确的指导,说明接下来我可以把指尖放在哪里。
我喜欢:name& :指标是一行。对于每个:量化有很多:结果。我希望结果能够作为一个列在相应量化下的页面下来。
这是我目前的表格:
但这就是我希望它表现的方式:
每增加一个新的量化,而不是显示在页面底部,我希望它横向显示,而不是垂直显示。
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
&#13;
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
&#13;
class QuantifiedsController < ApplicationController
before_action :set_quantified, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@quantifieds = Quantified.joins(:results).all
@averaged_quantifieds = current_user.quantifieds.averaged
@instance_quantifieds = current_user.quantifieds.instance
end
def show
end
def new
@quantified = current_user.quantifieds.build
end
def edit
end
def create
@quantified = current_user.quantifieds.build(quantified_params)
if @quantified.save
redirect_to quantifieds_url, notice: 'Quantified was successfully created'
else
render action: 'new'
end
end
def update
if @quantified.update(quantified_params)
redirect_to quantifieds_url, notice: 'Goal was successfully updated'
else
render action: 'edit'
end
end
def destroy
@quantified.destroy
redirect_to quantifieds_url
end
private
def set_quantified
@quantified = Quantified.find(params[:id])
end
def correct_user
@quantified = current_user.quantifieds.find_by(id: params[:id])
redirect_to quantifieds_path, notice: "Not authorized to edit this goal" if @quantified.nil?
end
def quantified_params
params.require(:quantified).permit(:categories, :name, :metric, :result, :date, results_attributes: [:id, :result_value, :date_value, :_destroy])
end
end
&#13;
或者我一直在玩索引视图我已经尝试了各种各样的标签和html标签,但我永远无法让代码看起来像上面那样,这就是为什么我认为答案可能会更深入。
<!-- 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>
&#13;
提前感谢您的帮助:)