Reputation: 2866
None of the above attributes are showing when I load the index page.
I know its because of this line: <% if averaged.user == current_user %>
More specifically it's because of the nested attributes of date_value and result_value (because if I take out those nested attributes the name & metric will show)
What do I need to add to the controller to allow the nested attributes to show on the index page, and in effect all the attributes?
Thanks in advance for your service!
<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>
controller
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
Please let me know if you need any more code or comments to help bring this question to a resolution.
Upvotes: 0
Views: 279
Reputation: 945
This will hopefully guide you in the right direction
This is odd to me
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
You define both of these twice and essentially overwrite each other?
If your attributes aren't showing, it doesn't have anything to do with the nesting, it has to do with the logic. So in other words, this logic
if averaged.user == current_user
is not evaluating to true, so check what each object is actually holding. You can do this in a dirty way by just inspecting it in the view
e.g. put <%= averaged.user.inspect
%> and <%= current_user.inspect %>
Once you know what objects it's holding, go backwards in your code to how you altered them to this state. I suspect it'll go back to the controller where you have some issues.
Upvotes: 1