如何将 Current_User 添加到嵌套属性?
How to Add Current_User to Nested Attribute?
- 名字
- 公制
- date_value
- result_value
None 以上属性在我加载索引页时显示。
我知道是因为这一行:<% 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>
控制器
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
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
如果您需要更多代码或评论来帮助解决此问题,请告诉我。
希望这能为您指明正确的方向
这对我来说很奇怪
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
未计算为真,因此请检查每个对象实际包含的内容。您可以通过在视图中检查它来以肮脏的方式执行此操作
例如放 <%= averaged.user.inspect
%> 和 <%= current_user.inspect %>
一旦您知道它持有什么对象,请在您的代码中回溯到您如何将它们更改为该状态。我怀疑它会返回到您遇到问题的控制器。
- 名字
- 公制
- date_value
- result_value
None 以上属性在我加载索引页时显示。
我知道是因为这一行:<% 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>
控制器
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
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
如果您需要更多代码或评论来帮助解决此问题,请告诉我。
希望这能为您指明正确的方向
这对我来说很奇怪
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
未计算为真,因此请检查每个对象实际包含的内容。您可以通过在视图中检查它来以肮脏的方式执行此操作
例如放 <%= averaged.user.inspect
%> 和 <%= current_user.inspect %>
一旦您知道它持有什么对象,请在您的代码中回溯到您如何将它们更改为该状态。我怀疑它会返回到您遇到问题的控制器。