質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ソート

複数のデータを、順序性に従って並べ替えること。 データ処理を行う際に頻繁に用いられ、多くのアルゴリズムが存在します。速度、容量、複雑さなどに違いがあり、高速性に特化したものにクイックソートがあります。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

720閲覧

ransackでのソート機能(並び替え機能)で並び順が変更されない。

kokemaru2427

総合スコア8

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ソート

複数のデータを、順序性に従って並べ替えること。 データ処理を行う際に頻繁に用いられ、多くのアルゴリズムが存在します。速度、容量、複雑さなどに違いがあり、高速性に特化したものにクイックソートがあります。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2021/02/28 01:47

前提・実現したいこと

プログラミングスクールに通うプログラミング初心者です。
現在、ruby on rails でイベント投稿アプリを開発しています。
このアプリで、投稿をソート(並び替え表示)できるようにしようとしており
ransackで実装しています。
ransackでの検索機能は実装済みで、検索機能はうまく動くのですが、ソート機能がうまく動かずに困っています。

ソートが変更されない画像
![eventsテーブル

並び替えをしても、並び順が変更されない。

該当のソースコード

(routes.rb)

Rails.application.routes.draw do devise_for :users root to: 'home#top' resources :users resources :events do resources :comments, only: :create resources :entries, only: [:index, :new, :create] resource :favorites, only: [:create, :destroy] collection do get 'search' end end resources :messages, only: :create resources :rooms, only: [:create, :show] end

(events_controller.rb)

class EventsController < ApplicationController before_action :move_to_index, except: [:index, :show] before_action :search_product, only: [:index, :search] def index @events = Event.all end def search @results = @p.result end private def move_to_index unless user_signed_in? redirect_to action: :index end end def search_product @p = Event.ransack(params[:q]) end end

(events/index.html.erb)

<div class="container"> <div class="row"> <%= search_form_for @p, url: search_events_path do |f| %> <%= f.label :title_cont, 'キーワード検索:' %> <%= f.search_field :title_cont %> <br> <%= f.label :event_date_gteq, '開催日で絞り込む:' %> <%= f.date_field :event_date_gteq %> ~ <%= f.date_field :event_date_lteq_end_of_day %> <br> <%= f.label :event_date_eq, '開催日選択:' %> <%= f.date_field :event_date_eq %> <br> <%= f.label :category_id_eq, 'カテゴリー:' %> <%= f.collection_select :category_id_eq, Category.all, :id, :name, include_blank: '指定なし' %> <br> <%= f.label :event_prefecture_id_eq, '開催県:' %> <%= f.collection_select :event_prefecture_id_eq, EventPrefecture.all, :id, :name, include_blank: '指定なし'%> <br> <%= f.label :cost, '価格:' %> <%= f.radio_button :cost_lteq, '' %> 指定なし <%= f.radio_button :cost_lteq, '0' %> 無料 <%= f.radio_button :cost_lteq, '500' %> 500円以内 <%= f.radio_button :cost_lteq, '1000' %> 1000円以内 <%= f.radio_button :cost_lteq, '1500' %> 1500円以内 <%= f.radio_button :cost_lteq, '2000' %> 2000円以内 <%= f.radio_button :cost_lteq, '3000' %> 3000円以内 <%= f.radio_button :cost_lteq, '4000' %> 4000円以内 <%= f.radio_button :cost_lteq, '5000' %> 5000円以内 <br> <%= link_to '全てを表示', events_path %> <br> <%= f.submit '検索' %> <% end %> <thead> <tr> <th><%= sort_link(@p, :id, "id") %></th> <th><%= sort_link(@p, :category_id, "カテゴリー") %></th> <th><%= sort_link(@p, :cost, "費用") %></th> <th colspan="3"></th> </tr> </thead> <% @events.each do |event| %> <div class="card mt-5 border mx-auto" style="width: 20rem;"> <div class="article-text"> <%= link_to event_path(event) do %> <%= attachment_image_tag event, :event_image, class: "card-img-top" , fallback: "no-image.jpg" %> </div> <div class="card-body"> <div class="article-title"> タイトル:<%= event.title %> </div> <div class="article-text"> 詳細:<%= event.details %> </div> <div class="article-genre"> ジャンル:<%= event.category.name %> </div> <div class="article-title"> 開催エリア:<%= event.event_prefecture.name %> </div> <div class="article-text"> 参加可能人数:<%= event.people_number.name %> </div> <div class="article-title"> 参加費用:<%= event.cost %> </div> <div class="article-text"> イベント日時:<%= event.event_date %> </div> <div class="article-text"> 開催住所:<%= event.event_address %> </div> <div class="article-text"> 開始時間:<%= event.start_time %> </div> <div class="article-text"> 終了時間:<%= event.end_time %> </div> <div class="article-text"> 主催者:<%= event.user.nickname %> </div> <div> <time class="mh-100"> <%= event.updated_at.strftime("%Y-%m-%d %H:%M") %> </time>更新 </div> </div> <% end %> <p> <% if current_user.already_favorited?(event) %> <%= link_to event_favorites_path(event), method: :delete do %> <i class="fas fa-heart"></i> <% end %> <% else %> <%= link_to event_favorites_path(event), method: :post do %> <i class="far fa-heart"></i> <% end %> <% end %> <%= event.favorites.count %></p> </div> <% end %> </div> </div>

(events/search.html.erb)は
(index.html.erb)と全く同じです。

(event.rb)モデル

class Event < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :category belongs_to :event_prefecture belongs_to :people_number attachment :event_image belongs_to :user has_many :comments, dependent: :destroy has_many :entries, dependent: :destroy has_many :favorites, dependent: :destroy end

補足

ransackの検索機能は動いていますが、ソート機能は動かないです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

events.controller.rbを
def index
@events = Event.all
@events = @p.result(distinct: true).order("created_at DESC")
end
とすることで解決。

投稿2021/03/01 02:58

kokemaru2427

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問