加载索引页时,上述属性均未显示。
我知道它是因为这一行:<%if averaged.user == current_user%>
更具体地说,因为date_value和result_value的嵌套属性(因为如果我取出那些名称和指标将显示的嵌套属性)
我需要添加到控制器中以允许嵌套属性显示在索引页面上,实际上是所有属性?
提前感谢您的服务!
<div id="values" class="panel panel-default">
<div class="panel-heading"><h4><b>AVERAGE</b></h4></div>
<!-- Table -->
<table>
<% @averaged_quantifieds.each do |averaged| %>
<% if averaged.user == current_user %>
<th class="value">
<%= link_to edit_quantified_path(averaged) do %>
<%= averaged.name %>
<% end %>
(<%= averaged.metric %>)
</th>
<tbody class="value">
<td><%= averaged.date_value.strftime("%m-%Y") %></td>
<td><%= averaged.result_value %></td>
</tbody>
<% end %>
<% end %>
</table>
</div>
&#13;
控制器
class QuantifiedsController < ApplicationController
before_action :set_quantified, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@averaged_quantifieds = current_user.quantifieds.averaged
@instance_quantifieds = current_user.quantifieds.instance
@averaged_quantifieds = Result.all.order("date_value")
@instance_quantifieds = Result.all.order("date_value")
end
def show
end
def new
@quantified = current_user.quantifieds.build
@quantified = Quantified.new
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;
quantified.rb
class Quantified < ActiveRecord::Base
belongs_to :user
scope :averaged, -> { where(categories: 'averaged') }
scope :instance, -> { where(categories: 'instance') }
has_many :results
accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true
CATEGORIES = ['averaged', 'instance']
end
&#13;
如果您需要更多代码或评论以帮助解决此问题,请与我们联系。
答案 0 :(得分:1)
这有望引导您朝着正确的方向前进
这对我来说很奇怪
def index
@averaged_quantifieds = current_user.quantifieds.averaged
@instance_quantifieds = current_user.quantifieds.instance
@averaged_quantifieds = Result.all.order("date_value")
@instance_quantifieds = Result.all.order("date_value")
end
您将这两者定义为两次并基本上相互覆盖?
如果你的属性没有显示,它与嵌套没有任何关系,它与逻辑有关。换句话说,这个逻辑
if averaged.user == current_user
未评估为true,因此请检查每个对象实际持有的内容。您可以通过在视图中检查它来以脏的方式执行此操作
e.g。放<%= averaged.user.inspect
%&gt;和<%= current_user.inspect %>
一旦你知道它所持有的对象,请在代码中向后转到如何将它们更改为此状态。我怀疑它会回到你遇到问题的控制器。