【发布时间】:2015-01-05 22:21:32
【问题描述】:
当用户量化自己时,_form 为他们提供了两种分类结果的方法,可以是每月平均或一次性实例。我希望每个类别都有自己的表格,如下所示:
我如何使用范围来执行此操作,因为现在我从第一个代码 sn-p 中得到一个未定义的方法错误:@average_quantifieds = current_user.quantifieds.average & @instance_quantifieds = current_user.quantifieds.instance
quantifieds_controller.rb
class QuantifiedsController < ApplicationController
before_action :set_quantified, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@quantifieds = Quantified.all.order("date")
@average_quantifieds = current_user.quantifieds.average
@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: 'Goal 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)
end
end
quantified.rb
class Quantified < ActiveRecord::Base
belongs_to :user
CATEGORIES = ['Monthly Average', 'One-Time Instance']
end
索引
<table>
<% @average_quantifieds.each do |average| %>
....
<% end %>
</table>
<table>
<% @instance_quantifieds.each do |instance| %>
....
<% end %>
</table>
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :goals
has_many :values
has_many :quantifieds
end
【问题讨论】:
-
您能发布您的
user.rb文件吗?尤其是联想部分…… -
我按要求将其添加到底部。感谢您为我调查此问题!
-
所有人都发表长评论作为答案。基本上,这就是答案。
标签: ruby-on-rails scope html-table