简体   繁体   中英

Rails display chartkick graph only for particular user

This is my code to create a chartkick linechart for temperatures..... but the chart displayed displays the values entered by all the users and not just the user who is logged in.....

this is in app/views/temperature/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"%>

How do I modify the same for it to pick and display chart from data only this particular user has entered

This is my Temperature controller:

                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

You get temperatures from all users in this line:

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

If you just want to temperature for just one user then you have to mention that in your query like below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM