0
  • name
  • metric
  • date_value
  • result_value

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?

I am using the cocoon gem and the devise gem.

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

result.rb

class Result < ActiveRecord::Base
  belongs_to :quantified
 belongs_to :user
end

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
    has_many :results
end

Please let me know if you need any more code or comments to help bring this question to a resolution.

user229044
  • 232,980
  • 40
  • 330
  • 338
AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80
  • 2
    You have ```@averaged_quantifieds``` and ```@instance_quantifieds``` defined twice in your controller. Those repetitions will redefine your instance variables to the last definition for those instance variables you declare. In this case, @average_quantifieds will just be ```Result.all.order("date_value")``` and not contain any thing regarding the current user. – Kelsey Hannan Jan 24 '15 at 06:17
  • 2
    Yep, what @Kelseydh said! Delete those last two lines in the `index` method, or rename the variables :) – nathanvda Jan 24 '15 at 10:41
  • How do I rename them to include: current_user.quantifieds.averaged & Result.all.order("date_value")? Please respond in the answers so I can check it off. Thanks so much! – AnthonyGalli.com Jan 24 '15 at 17:51
  • I added: @quantifieds = Result.all.order("date_value")@averaged_quantifieds = current_user.quantifieds.averaged@instance_quantifieds = current_user.quantifieds.instance. But now I get an undefined method `date_value.' I don't know if this deserves a separate question entirely or if I am changing the controller wrongly. – AnthonyGalli.com Jan 24 '15 at 18:02
  • I think the error may warrant a new question: http://stackoverflow.com/questions/28129128/nomethoderror-in-index-how-to-define-the-nested-attributes – AnthonyGalli.com Jan 24 '15 at 19:01

0 Answers0