这是我为温度创建图表线图的代码..... 但显示的图表显示所有用户输入的值,而不仅仅是登录的用户.....
这是在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"%>
如何修改相同内容以便仅从此特定用户输入的数据中选择和显示图表
这是我的温度控制器:
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
答案 0 :(得分:0)
您可以获得此行中所有用户的温度:
<%= line_chart Temperature.group("date(temperatures.date)").average(:temperature),xtitle: "Date", ytitle: "Temperature"%>
如果您只想为一个用户提供温度,那么您必须在下面的查询中提及:
<%= line_chart Temperature.where(:user_id => current_user.id).group("date(temperatures.date)").average(:temperature),xtitle: "Date", ytitle: "Temperature"%>