アイテムのあるお店があります。各アイテムには価格があります。
価格を昇順で並べ替え、価格ヘッダーの価格を降順で並べ替えてみました。価格をクリックすると、$ 10〜 $ 50のすべてのアイテムが正しく並べ替えられますが、$ 10未満で$ 50を超えるものは並べ替えられません。
これが私のコードです:
class TeethersController < ApplicationController
before_action :authenticate_user!,except: [:index,:show]
helper_method :sort_column, :sort_direction
before_action :find_teether, only: [:destroy,:edit,:update,:show]
def index
@teethers= Teether.all.order(sort_column + ' ' + sort_direction)
@colors = Array.new
@types = Array.new
@teethers.each {|t| @types << t.types.pluck(:name) }
@teethers.each {|t| @colors << t.colors.pluck(:name) }
if params[:search]
@search_term = params[:search]
@teethers= @teethers.search_by(@search_term)
end
if params[:type_id]
@types = Typation.where(type_id: params[:type_id])
@teethers = @types.map(&:teether)
end
end
def create
@teether = Teether.new(teether_attributes)
if @teether.save
redirect_to teethers_path, notice: "Thank you... Your teether information was created successfully."
else
flash.now[:error] = "Please correct the form"
render :new
end
end
def new
@teether=Teether.new
end
private
def find_teether
@teether = Teether.find(params[:id])
end
def sort_column
Teether.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
def teether_attributes
teether_attributes = params.require(:teether).permit([:name,:description,:price,:image,:keywords,:status,:quantity,:discount,:kind,{type_ids: []},:gender,:color,:theme])
end
end
私のアプリケーションヘルパーは次のとおりです。
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
direction = (column == sort_column && sort_direction == "asc")? "desc" : "asc"
link_to title, :sort => column, :direction => direction
end
end
これは私のインデックスにあります:
<h3 id="sort"><label>Sort: </label><span> | </span><span><%= sortable "price" %><span class="caret"></span></span></h3>
データベース内
class CreateTeethers < ActiveRecord::Migration[5.2]
def change
create_table :teethers do |t|
t.string :name
t.text :description
t.string :price
t.timestamps
end
end
end
class AddDiscountToTeether < ActiveRecord::Migration[5.2]
def change
add_column :teethers, :discount, :text
add_column :teethers, :kind, :text
end
end
価格を10進数に変更します(または整数...セント...そして「お金」の宝石を使用します。これは素晴らしい宝石です!)。
移行によって文字列を10進数に変更できるとは思いませんが、これがまだデプロイされていない開発である場合は、問題ではありません。
したがって、remove_column'value 'および' discount 'に移行します
次に、10進フィールドとしてadd_column値に移行し、10進フィールドとして割引を行います。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加