-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/sam/add ransack date and sort #4
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bowdensl712 いったん途中まで見ましたー
def set_search | ||
search_options = { | ||
created_after: params[:created_after], | ||
created_before: params[:created_before] | ||
} | ||
@q = Article.ransack(params[:q]) | ||
@article_results = @q.result(distinct: true) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArticlesController
ではなく、 ApplicationController
に記述をしている理由を知りたいですー
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
articles#indexに@qと@article_resultsを設定しようとした時に、"No Ransack::Search object was provided to search_form_for!"というエラーが発生しました。エラーを調べた結果、こちらのIssue(https://github.com/activerecord-hackery/ransack/issues/3)で"ApplicationControllerに移せば治るよ"という返答がありましたので、それに従いました。
ただいま改めて試した結果、set_searchメソッドをArticlesControllerに移し、"before_action :set_search"をApplicationControllerに残せば治りました。
また別件、search_optionsの記述は不必要らしいので、削除いたしました。
@@ -1,22 +1,22 @@ | |||
class CommentsController < ApplicationController | |||
http_basic_authenticate_with name: "shh", password: "secret", only: :destroy | |||
http_basic_authenticate_with name: 'shh', password: 'secret', only: :destroy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この認証処理は全てのControllerに記述することになりそうなので、 ApplicationController
に書いてしまって良さそう
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正いたしました!
params.require(:article).permit(:title, :body, :image_link, :language, :status) | ||
end | ||
|
||
def search |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Articleの一覧表示の時も、検索をする時も、どちらの場合も ArticleController#index
を利用出来ないか確認してみてほしいですー
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最初にRansackを導入した際に、結果が自動的にIndexで表示されておりましたが、IndexとResultページを分けた方が勉強になり、よりcustomizeできると思いました。今後はIndexに統一いたします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indexで検索結果を表示するように修正いたしました。
app/models/article.rb
Outdated
end | ||
scope :created_after, ->(time) { | ||
# time = year.to_s + "-" + month.to_s + "-" + day.to_s | ||
time = time.to_time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Timezoneの問題を発生させないようにしたいので
- time = time.to_time
+ time = Time.zone.parse(time)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
かしこまりました!修正いたします。
app/models/article.rb
Outdated
scope :created_before, ->(time) { | ||
# time = year.to_s + "-" + month.to_s + "-" + day.to_s | ||
time = time.to_time | ||
time = time + 23.hours + 59.minutes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下では意図しない動きになってしまいますか?
- time = time + 23.hours + 59.minutes
+ time = time + 1.day
or
+ time = time + 24.hours
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらの場合は、選択した日付の全日(00:00〜23:59)の投稿を選択するために追加いたしましたので、1.dayや24.hoursでは1分の誤差(翌日の00:00)が入るかと存ます。
例:
○ 2023-02-17 00:00:00 UTC〜2023-02-17 23:59:00 UTC
X 2023-02-17 00:00:00 UTC〜2023-02-18 00:00:00 UTC
こちらは間違っておりますでしょうか?
app/views/articles/_footer.html.erb
Outdated
@@ -1,4 +1,3 @@ | |||
<footer> | |||
|
|||
<i><a href="<%= root_path %>" id="footer_link">Postblog Inc.</a> Reserved 2023</i> | |||
<i><a href="<%= root_path %>" id="footer_link">Postblog Inc.</a> Reserved 2023</i> | |||
</footer> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ファイルの最後の行には改行を入れてもらえると!
VScodeの設定で自動で改行を入れることが出来るよー
https://qiita.com/norikt/items/83674fadd79a88bf7824
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
承知しました!設定を有効にしました!
<%= form.label :language %><br> | ||
<%= form.select :language, ['English', 'Japanese'] %> | ||
<%= form.label :language %><br> | ||
<%= form.select :language, ['English', 'Japanese'] %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
['English', 'Japanese']
の部分もemumで管理すると良さそう
https://api.rubyonrails.org/v6.0.2.2/classes/ActiveRecord/Enum.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enumに変更いたしました!
app/views/articles/_form.html.erb
Outdated
<%= form.label :status %><br> | ||
<%= form.select :status, ['public', 'private', 'archived']%> | ||
<%= form.label :status %><br> | ||
<%= form.select :status, ['public', 'private', 'archived']%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
statusはモデル側でenumで管理してあげると幸せになれそう
https://api.rubyonrails.org/v6.0.2.2/classes/ActiveRecord/Enum.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Active Recordで既に:publicと:privateのclass methodを定義していたらしく、以下のエラーが発生しました。
You tried to define an enum named "status" on the model "Article", but this will generate a class method "public", which is already defined by Active Record.
しかし、enumの名称をpublic_article, private_article, archived_articleに変更したら、使えるようになりました。前の名称を指す部分(_form.html.erb等)も新しい名称に修正いたしました。
残念ながら、:statusと:language欄をstring → integerに変更する際に、既存のデータを数値化する方法が見つからず、欄内のデータを無くしました。今回は少数の記事で再入力が容易でしたが、データを無くさずにenum数値に変換する方法はございますか?
…dex.html.erbで表示 ・articles#set_searchメソッドを削除 ・results.html.erbを削除 ・_search.html.erbにcreated_after検索フィールドのデフォルト日付を2023-02-01に変更
07f6450
to
a0d8d38
Compare
:created_afterと:created_beforeスコープを修正
Ransackの日付検索と並び替え機能を追加しました。
・Controller, Model, Viewsでのスタイルエラーも修正しました。