如何在过滤后更新页面上的图表(Chartkick)?
How to update chart (Chartkick) on a page after filtering?
我有问题。我使用 ransack 制作了一些过滤器并且它们工作正常,但我不知道如何更新我在我过滤的 table 下实现的饼图。该图表是使用 Chartkick 制作的。一般来说,我对 ror 和编程还很陌生。这是我的索引文件:
<h1>Lista Przychodów</h1>
<h2><%= link_to 'Nowy Przychód', new_income_path %></h2>
<%= search_form_for @q do |f| %>
<div class="field">
<%= f.label :title_cont, "Tytuł zawiera" %>
<%= f.search_field :title_cont %>
</div>
<div class="field">
<%= f.label :text_cont, "Opis zawiera" %>
<%= f.search_field :text_cont %>
</div>
<div class="field">
<%= f.label :category_name_cont, "Kategoria zawiera" %>
<%= f.search_field :category_name_cont %>
</div>
<div class="field">
<%= f.label :amount_gteq, "Kwota pomiędzy" %>
<%= f.search_field :amount_gteq %>
<%= f.label :amount_lteq, "a" %>
<%= f.search_field :amount_lteq %>
</div>
<%= f.submit "Szukaj" %>
<div class="button">
<%= link_to 'Wyczyść filtry', request.path, class:"cancel-button" %>
</div>
<% end %>
<div style="overflow-x:auto;">
<table class="center">
<tr>
<th><%= sort_link(@q, :title, 'Tytuł', default_order: :desc) %></th>
<th><%= sort_link(@q, :text, 'Opis') %></th>
<th><%= sort_link(@q, :amount, 'Kwota') %></th>
<th><%= sort_link(@q, :category_id, 'Kategoria') %></th>
<th colspan="3"></th>
</tr>
<% @incomes.each do |income| %>
<tr>
<td><%= income.title %></td>
<td><%= income.text %></td>
<td><%= number_to_currency(income.amount, :unit => "zł", :format => "%n %u") %></td>
<td><%= Category.find(income.category_id).name %></td>
<td><%= link_to 'Pokaż', income_path(income) %></td>
<td><%= link_to 'Edytuj', edit_income_path(income) %></td>
<td><%= link_to 'Usuń', income_path(income),
method: :delete,
data: { confirm: 'Czy na pewno chcesz usunąć ten wpis?' } %></td>
</tr>
<% end %>
</table>
</div>
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
<footer><%= link_to 'Strona główna', controller: 'welcome' %></footer>
如果需要的话还有我的控制器文件:
class IncomesController < ApplicationController
helper_method :sort_column, :sort_direction
http_basic_authenticate_with name: "admin", password: "admin", except: [:index, :show]
def index
@q = Income.search(params[:q])
@incomes = @q.result.includes(:category)
end
def show
@income = Income.find(params[:id])
end
def new
@income = Income.new
@categories = Category.all.map { |c| [c.name, c.id]}
end
def edit
@income = Income.find(params[:id])
@categories = Category.all.map { |c| [c.name, c.id]}
end
def create
@income = Income.new(income_params)
@categories = Category.all.map { |c| [c.name, c.id]}
if @income.save
redirect_to @income
else
render 'new'
end
end
def update
@income = Income.find(params[:id])
if @income.update(income_params)
redirect_to @income
else
render 'edit'
end
end
def destroy
@income = Income.find(params[:id])
@income.destroy
redirect_to incomes_path
end
private
def income_params
params.require(:income).permit(:title, :text, :amount, :category_id)
end
def sort_column
params[:sort] || "title"
end
def sort_direction
params[:direction] || "asc"
end
end
我已经在互联网上查找过类似的问题,但没有找到任何内容。我将不胜感激一些帮助,或者至少一些指导如何做。我真的迷上了 ror 并想了解更多关于它的信息,这就是我来这里寻求帮助的原因。预先感谢您的回答。
如果您的意思是过滤器应用于 table 但未应用于图表,这是因为您仅对图表进行另一个查询,在这一行中:
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
您应该使用具有过滤数据的相同实例变量:
@incomes
所以它应该是这样的:
<%= pie_chart @incomes.sum(:amount), suffix: " zł", thousands: ",", messages: { empty: "Nie ma wpisów" }, download: { filename: "wykres" }, title: "Podział przychodów na kategorie" %>
我有问题。我使用 ransack 制作了一些过滤器并且它们工作正常,但我不知道如何更新我在我过滤的 table 下实现的饼图。该图表是使用 Chartkick 制作的。一般来说,我对 ror 和编程还很陌生。这是我的索引文件:
<h1>Lista Przychodów</h1>
<h2><%= link_to 'Nowy Przychód', new_income_path %></h2>
<%= search_form_for @q do |f| %>
<div class="field">
<%= f.label :title_cont, "Tytuł zawiera" %>
<%= f.search_field :title_cont %>
</div>
<div class="field">
<%= f.label :text_cont, "Opis zawiera" %>
<%= f.search_field :text_cont %>
</div>
<div class="field">
<%= f.label :category_name_cont, "Kategoria zawiera" %>
<%= f.search_field :category_name_cont %>
</div>
<div class="field">
<%= f.label :amount_gteq, "Kwota pomiędzy" %>
<%= f.search_field :amount_gteq %>
<%= f.label :amount_lteq, "a" %>
<%= f.search_field :amount_lteq %>
</div>
<%= f.submit "Szukaj" %>
<div class="button">
<%= link_to 'Wyczyść filtry', request.path, class:"cancel-button" %>
</div>
<% end %>
<div style="overflow-x:auto;">
<table class="center">
<tr>
<th><%= sort_link(@q, :title, 'Tytuł', default_order: :desc) %></th>
<th><%= sort_link(@q, :text, 'Opis') %></th>
<th><%= sort_link(@q, :amount, 'Kwota') %></th>
<th><%= sort_link(@q, :category_id, 'Kategoria') %></th>
<th colspan="3"></th>
</tr>
<% @incomes.each do |income| %>
<tr>
<td><%= income.title %></td>
<td><%= income.text %></td>
<td><%= number_to_currency(income.amount, :unit => "zł", :format => "%n %u") %></td>
<td><%= Category.find(income.category_id).name %></td>
<td><%= link_to 'Pokaż', income_path(income) %></td>
<td><%= link_to 'Edytuj', edit_income_path(income) %></td>
<td><%= link_to 'Usuń', income_path(income),
method: :delete,
data: { confirm: 'Czy na pewno chcesz usunąć ten wpis?' } %></td>
</tr>
<% end %>
</table>
</div>
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
<footer><%= link_to 'Strona główna', controller: 'welcome' %></footer>
如果需要的话还有我的控制器文件:
class IncomesController < ApplicationController
helper_method :sort_column, :sort_direction
http_basic_authenticate_with name: "admin", password: "admin", except: [:index, :show]
def index
@q = Income.search(params[:q])
@incomes = @q.result.includes(:category)
end
def show
@income = Income.find(params[:id])
end
def new
@income = Income.new
@categories = Category.all.map { |c| [c.name, c.id]}
end
def edit
@income = Income.find(params[:id])
@categories = Category.all.map { |c| [c.name, c.id]}
end
def create
@income = Income.new(income_params)
@categories = Category.all.map { |c| [c.name, c.id]}
if @income.save
redirect_to @income
else
render 'new'
end
end
def update
@income = Income.find(params[:id])
if @income.update(income_params)
redirect_to @income
else
render 'edit'
end
end
def destroy
@income = Income.find(params[:id])
@income.destroy
redirect_to incomes_path
end
private
def income_params
params.require(:income).permit(:title, :text, :amount, :category_id)
end
def sort_column
params[:sort] || "title"
end
def sort_direction
params[:direction] || "asc"
end
end
我已经在互联网上查找过类似的问题,但没有找到任何内容。我将不胜感激一些帮助,或者至少一些指导如何做。我真的迷上了 ror 并想了解更多关于它的信息,这就是我来这里寻求帮助的原因。预先感谢您的回答。
如果您的意思是过滤器应用于 table 但未应用于图表,这是因为您仅对图表进行另一个查询,在这一行中:
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
您应该使用具有过滤数据的相同实例变量:
@incomes
所以它应该是这样的:
<%= pie_chart @incomes.sum(:amount), suffix: " zł", thousands: ",", messages: { empty: "Nie ma wpisów" }, download: { filename: "wykres" }, title: "Podział przychodów na kategorie" %>