現在 girb 資料有這 8 個分類
Dead Programmer
Successful Programmer
Famous Programmer
Working Programmer
Average Programmer
Amateur Programmer
Unknown Programmer
Bad Programmer
來做個過濾器吧
app/views/layouts/applicaton.html.erb
<title>Girbling</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<!-- start of menu -->
<div class = "navbar navbar-inverse navbar-static-top">
<div class = "container">
<a href="#" class = "navbar-brand active">Girb</a>
<button class = "navbar-toggle" data-toggle = "collapse" data-target = ".navHeaderCollapse">
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
<span class = "icon-bar"></span>
</button>
<div class = "collapse navbar-collapse navHeaderCollapse">
<ul class="nav navbar-nav navbar-right">
<li class = "dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Fun1<b class="caret"></b></a>
<ul class = "dropdown-menu">
<li><%= link_to "Dead", girbs_path(level_cd: "0") %></li>
<li><%= link_to "Successful", girbs_path(level_cd: "1") %></li>
<li><%= link_to "Famous", girbs_path(level_cd: "2") %></li>
<li><%= link_to "Working", girbs_path(level_cd: "3") %></li>
<li><%= link_to "Average", girbs_path(level_cd: "4") %></li>
<li><%= link_to "Amateur", girbs_path(level_cd: "5") %></li>
<li><%= link_to "Unknown", girbs_path(level_cd: "6") %></li>
<li><%= link_to "Bad", girbs_path(level_cd: "7") %></li>
</ul>
</li>
<li class = "dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Fun2<b class="caret"></b></a>
<ul class = "dropdown-menu">
<li><a href="#">Like</a></li>
<li><a href="#">Love</a></li>
<li><a href="#">Hate</a></li>
<li><a href="#">Love</a></li>
</ul>
</li>
<li><a href="#">Fun3</a></li>
<li><a href="#">Fun4</a></li>
</ul>
</div>
</div>
</div>
<!-- end of menu -->
<%= yield %>
<!-- start of footer -->
<div class = "navbar navbar-default navbar-fixed-bottom">
<div class = "container">
<sapn class = "navbar-text">Site Built By Hazetodo </span>
<%= link_to "Bilink", girbs_path, :class => "navbar-btn btn-danger btn btn-sm" %>
<%= link_to new_girb_path, :class => "navbar-btn btn btn-primary btn-sm " do %>
<i class=" fa fa-plus fa-sm"></i> New
<% end %>
<%= link_to raw('<i class="fa fa-plus fa-sm"></i> New'), new_girb_path, :class => "navbar-btn btn btn-warning btn-sm" %>
</div>
</div>
<!-- end of footer -->
app/views/girbs/index.html.erb
<div class="container">
<h1>Index Page of Girb ( <%= @girbs.count %> )
<% if @girbs.count != Girb.all.count %>( Filter: <%= @girbs.first.level %> ) <% end %></h1>
<div>
<span class="pull-right">
<%= link_to girbs_path(tab: "asc") do %>
<i class="fa fa-sort-alpha-asc"></i>
<% end %>
<%= link_to girbs_path(tab: "desc") do %>
<i class="fa fa-sort-alpha-desc"></i>
<% end %>
<% if @at_like == nil %>
<%= link_to like_asc_girbs_path do %>
<i class="fa fa-child"></i>
<% end %>
<% elsif @at_like %>
<%= link_to like_asc_girbs_path do %>
<i class="fa fa-male" style="color:blue"></i>
<% end %>
<% else %>
<%= link_to like_desc_girbs_path do %>
<i class="fa fa-female" style="color:red"></i>
<% end %>
<% end %>
</span>
</div>
<%= form_tag(girbs_path, method: :get, id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Name" %>
<%= submit_tag "Search", name: nil %>
<% end %>
<p></p>
<div class="row">
<% @girbs.each do |girb| %>
<div class="col-lg-4 col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<%= link_to girb_path(girb) do %>
<div style="font-size:22px;color:#0099FF">
<%= girb.name %>
<span>
<%= link_to edit_girb_path(girb) do %>
<i class="fa fa-pencil-square-o" style="color:green"></i>
<% end %>
</span>
<span>
<%= link_to girb_path(girb), method: :delete, data: {confirm: "Are you sure you want to delete?"} do %>
<i class="fa fa-trash" style="color:purple"></i>
<% end %>
</span>
</div>
<% end %>
<div ><%= girb.email %></div>
<div style="padding-top:3px">
<%= link_to like_girb_path(girb.id), method: :post do %>
<i class="fa fa-heart" style="color:red"> <%= girb.like %></i>
<% end %>
<span class="pull-right">
<%= link_to on_click_bang_girb_path(girb.id), method: :post do %>
<i class="fa fa-rocket" > <%= girb.bangs.size %></i>
<% end %>
</span>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
app/controllers/girbs_controller.rb
class GirbsController < ApplicationController
def index
@tab = params[:tab] if params[:tab].present?
@level_cd = params[:level_cd] if params[:level_cd].present?
if params[:search]
@girbs = Girb.search(params[:search])
else
if @tab == "asc"
if @level_cd
@girbs = Girb.all.where(level_cd: @level_cd).order("name ASC")
else
@girbs = Girb.all.order("name ASC")
end
elsif @tab == "desc"
if @level_cd != nil
@girbs = Girb.all.where(level_cd: @level_cd).order("name DESC")
else
@girbs = Girb.all.order("name DESC")
end
else
if @level_cd
@girbs = Girb.all.where(level_cd: @level_cd)
else
@girbs = Girb.all
end
end
end
end
# def index
# @tab = if %w(asc desc).include?(params[:tab]) then params[:tab] else :asc end
# @girbs = Girb.level_list(params[:level_cd]).search(params[:search]).order(name: @tab)
# end
def like_asc
@at_like = false
@girbs = Girb.all.order("like ASC")
render :index
end
def like_desc
@at_like = true
@girbs = Girb.all.order("like DESC")
render :index
end
def like
@girb = Girb.find(params[:id])
@girb.like += 1;
@girb.save
# redirect_to(girbs_path)
redirect_to :back
end
def on_click_bang
@girb = Girb.find(params[:id])
@girb.bangs.create
# redirect_to(girbs_path)
redirect_to :back
end
def new
@girb = Girb.new
end
def create
@girb = Girb.new(girb_params)
if @girb.save
redirect_to(girbs_path)
else
render :new
end
end
def show
@girb = Girb.find(params[:id])
# @comment = @girb.comments.build
end
def edit
@girb = Girb.find(params[:id])
end
def update
@girb = Girb.find(params[:id])
if @girb.update(girb_params)
redirect_to girbs_path
else
render :edit
end
end
def destroy
@girb = Girb.find(params[:id])
@girb.destroy
redirect_to girbs_path
end
private
def girb_params
params.require(:girb).permit(:name, :email, :like, :level_cd)
end
end