2017-03-26 1 views
0

Dies ist mein Code, um ein Diagrammkick Liniendiagramm für Temperaturen zu erstellen ..... aber das Diagramm angezeigt zeigt die Werte von allen Benutzern eingegeben und nicht nur der Benutzer, der angemeldet ist. ....Rails Anzeige Chartkick Graph nur für bestimmte Benutzer

dies in app/views/Temperatur/index.html.erb

   <% if !flash[:notice].blank? %> 
        <div class="alert alert-info"> 
        <%= flash[:notice] %> 
        </div> 
       <% end %> 
       <br /> 

       <h1>Body Temperatures</h1> 
       <%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %> 
       <div style="border:2px solid black"> 
       <%= line_chart Temperature.group("date(temperatures.date)").average(:temperature),xtitle: "Date", ytitle: "Temperature"%> 
       </div> 
       <br> 
       <table class ="table table-striped"> 
        <thead class ="thead thead-default"> 
        <tr> 
         <th>Date</th> 
         <th>Temperature</th> 
         <th>Unit of Measure</th> 
         <th colspan="3"></th> 
        </tr> 
        </thead> 

        <tbody> 
        <% @temperatures.each do |temperature| %> 
         <tr> 
         <td><%= temperature.date.strftime("%m/%d/%Y") %></td> 
         <td><%= temperature.temperature %></td> 
         <td><%= temperature.measured %></td> 
         <td><%= link_to 'Show', temperature,class: 'btn btn-mini' %></td> 
         <td><%= link_to 'Edit', edit_temperature_path(temperature),class: 'btn btn-mini btn-success' %></td> 
         <td><%= link_to 'Destroy', temperature, method: :delete,class: 'btn btn-mini btn-danger', data: { confirm: 'Are you sure?' } %></td> 
         </tr> 
        <% end %> 
        </tbody> 
       </table> 

       <div id="paginator"> 
       <%= paginate @temperatures %> 
       </div> 

       <br> 

       <%= link_to 'New Temperature', new_temperature_path,class: "btn btn-secondary"%> 

Wie kann ich die gleiche ändern für ihn zu holen und hat nur diese bestimmten Benutzer von Datenanzeigekarte eingetragen

Dies ist mein Temperaturregler ler:

   class TemperaturesController < ApplicationController 
        before_action :authenticate_user! 
        before_action :set_temperature, only: [:show, :edit, :update, :destroy] 

        # GET /temperatures 
        # GET /temperatures.json 
        def index 
        @temperatures = current_user.temperatures.page(params[:page]).per(3) 
        end 

        # GET /temperatures/1 
        # GET /temperatures/1.json 
        def show 
        @temperatures = Temperature.find_by(user_id: current_user.id) if current_user 
        end 

        # GET /temperatures/new 
        def new 
        @temperature = current_user.temperatures.build 
        end 

        # GET /temperatures/1/edit 
        def edit 
        end 

        # POST /temperatures 
        # POST /temperatures.json 
        def create 
        @temperature = current_user.temperatures.build(temperature_params) 

        respond_to do |format| 
         if @temperature.save 
         format.html { redirect_to @temperature, notice: 'Temperature was successfully created.' } 
         format.json { render :show, status: :created, location: @temperature } 
         else 
         format.html { render :new } 
         format.json { render json: @temperature.errors, status: :unprocessable_entity } 
         end 
        end 
        end 

        # PATCH/PUT /temperatures/1 
        # PATCH/PUT /temperatures/1.json 
        def update 
        respond_to do |format| 
         if @temperature.update(temperature_params) 
         format.html { redirect_to @temperature, notice: 'Temperature was successfully updated.' } 
         format.json { render :show, status: :ok, location: @temperature } 
         else 
         format.html { render :edit } 
         format.json { render json: @temperature.errors, status: :unprocessable_entity } 
         end 
        end 
        end 

        # DELETE /temperatures/1 
        # DELETE /temperatures/1.json 
        def destroy 
        if current_user.id == @temperature.user.id 
         @temperature.destroy 
         respond_to do |format| 
         format.html { redirect_to temperatures_url, notice: 'Temperature was successfully destroyed.' } 
         format.json { head :no_content } 
         end 
        else 
        redirect_to root_path, notice: "You don't have permission." 
        end 
        end 

        private 
        # Use callbacks to share common setup or constraints between actions. 
        def set_temperature 
         @temperature = Temperature.find(params[:id]) 
        end 

        # Never trust parameters from the scary internet, only allow the white list through. 
        def temperature_params 
         params.require(:temperature).permit(:date, :temperature, :measured) 
        end 
       end 

Antwort

0

Sie erhalten Temperaturen von allen Benutzern in dieser Zeile:

<%= line_chart Temperature.group("date(temperatures.date)").average(:temperature),xtitle: "Date", ytitle: "Temperature"%> 

Wenn Sie nur für nur einen Benutzer auf die Temperatur, dann müssen Sie wie unten, dass in Ihrer Anfrage erwähnen:

<%= line_chart Temperature.where(:user_id => current_user.id).group("date(temperatures.date)").average(:temperature),xtitle: "Date", ytitle: "Temperature"%> 
+0

Das war extrem hilfreich! Ich danke dir sehr! –

Verwandte Themen