我想暂时隐藏一些声明的记录,并更新余额。
请参阅屏幕截图以便更好地理解,如下所示;
我想要的是,当我点击隐藏按钮时,它应该完全隐藏行,并且必须结转余额。当点击显示全部时,它必须显示所有记录。
我在表格中创建了隐藏字段,并在控制器中编写了hide_xvaziri方法但与hide button_to函数混淆,以便获取' / xvaziri /:id / hide',以:& #39;#xvaziris hide_xvaziri'
20160729062246_add_hidden_to_xvaziris.rb
class AddHiddenToXvaziris < ActiveRecord::Migration
def change
add_column :xvaziris, :hidden, :boolean, :default => false
end
end
xvaziris_controller.rb
class XvazirisController < ApplicationController
before_action :set_xvaziri, only: [:show, :edit, :update, :destroy]
def index
@xvaziris = Xvaziri.find_by hidden: false
@xvaziris = Xvaziri.search(params[:search])
respond_to do |format|
format.js
format.html
end
end
def import
Xvaziri.import(params[:file])
redirect_to xvaziris_url, notice: "Xvaziris imported."
end
def show
end
def new
@xvaziri = Xvaziri.new
end
def create
@xvaziri = Xvaziri.new(xvaziri)
if
@xvaziri.save
flash[:notice] = 'Xvaziri Created'
redirect_to @xvaziri
else
render 'new'
end
end
def edit
end
def update
if @xvaziri.update(xvaziri)
flash[:notice] = 'Xvaziri Updated'
redirect_to @xvaziri
else
render 'edit'
end
end
def destroy
@xvaziri.destroy
flash[:notice] = 'Xvaziri was successfully destroyed.'
redirect_to xvaziris_url
end
def hide_xvaziri
@xvaziri = Xvaziri.find(params[:id])
@xvaziri.hidden = true
flash[:notice] = 'Xvaziri was successfully hidden.'
redirect_to xvaziris_url
end
def reset_filter
xvaziris = Xvaziri.all
xvaziris.each do |xvaziri|
xvaziri.hidden = false
end
redirect_to xvaziris_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_xvaziri
@xvaziri = Xvaziri.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def xvaziri
params.require(:xvaziri).permit(:date, :description, :amount, :discount, :paid)
end
end
index.html.erb
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="table-responsive myTable">
<table id = "kola" class="table listing text-center">
<thead>
<tr class="tr-head">
<td>Description</td>
<td>Amount</td>
<td>Discount</td>
<td>Paid</td>
<td>Balance</td>
<td>Button</td>
</tr>
</thead>
<tbody>
<%= render @xvaziris %>
</tbody>
</table>
</div>
</div>
</div>
<%= link_to "Show all", xvaziri_resetfilter_path %>
_xvaziri.html.erb
<tr class="tr-<%= cycle('odd', 'even') %>">
<td class="col-3"><%= span_with_possibly_red_color xvaziri.description %></td>
<td class="col-1"><%= number_with_precision(xvaziri.amount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(xvaziri.discount, :delimiter => ",", :precision => 2) %></td>
<td class="col-1 neg"><%= number_with_precision(xvaziri.paid, :delimiter => ",", :precision => 2) %></td>
<% @balance += xvaziri.amount.to_f - xvaziri.discount.to_f - xvaziri.paid.to_f %>
<% color = @balance >= 0 ? "pos" : "neg" %>
<td class="col-1 <%= color %>"><%= number_with_precision(@balance.abs, :delimiter => ",", :precision => 2) %></td>
<td class="col-1"><%= button_to "Hide", '#', :method => "get" %></td>
</tr>
的routes.rb
Rails.application.routes.draw do
root 'xvaziris#index'
resources :xvaziris do
collection { post :import }
end
get '/xvaziri/:id/hide', to: 'xvaziris#hide_xvaziri'
get '/xvaziri/resetfilter', to: 'xvaziris#reset_filter'
end
欢迎任何建议。
提前谢谢。
答案 0 :(得分:1)
你为什么要尝试用rails做这个?
你想要的是在前端隐藏/显示元素。
更好的方法是使用javascript / jquery
这里有很多例子:
http://www.randomsnippets.com/2011/04/10/how-to-hide-show-or-toggle-your-div-with-jquery/
在这里你可以看一下jquery文档: