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

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

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

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

Q&A

解決済

1回答

1375閲覧

Rails 投稿ページを作っているが、常に投稿日がログインした日付になっている。

sattaku1538

総合スコア7

Ruby on Rails

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

0グッド

0クリップ

投稿2021/11/09 23:25

編集2021/11/10 01:00

イメージ説明

Ruby on Railsで投稿記事ページを作成しています。
テスト投稿をすると、投稿日が常にログインした日付(今日の日付)になっていまいます。
(4日前に投稿した記事が常にその日の投稿日になります。)

どこの部分が問題かわかりません。(db, View, Config....?)

原因を教えてください。

※追記

(schema.rb) ActiveRecord::Schema.define(version: 2021_11_05_042913) do create_table "book_comments", force: :cascade do |t| t.integer "user_id" t.integer "book_id" t.text "comment" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "books", force: :cascade do |t| t.string "title" t.text "body" t.integer "user_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "favorites", force: :cascade do |t| t.integer "user_id" t.integer "book_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "relationships", force: :cascade do |t| t.integer "follower_id" t.integer "followed_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.string "name" t.text "introduction" t.string "profile_image_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end
(users_controller.rb) class UsersController < ApplicationController before_action :authenticate_user! before_action :ensure_correct_user, only: [:edit, :update] def show @user = User.find(params[:id]) @books = @user.books @book = Book.new @today_book = @books.created_today @yesterday_book = @books.created_yesterday @this_week_book = @books.created_this_week @last_week_book = @books.created_last_week end def index @users = User.all @book = Book.new end def edit @user = User.find(params[:id]) end def update if @user.update(user_params) redirect_to user_path(@user), notice: "You have updated user successfully." else render "edit" end end private def user_params params.require(:user).permit(:name, :introduction, :profile_image) end def ensure_correct_user @user = User.find(params[:id]) unless @user == current_user redirect_to user_path(current_user) end end end
(books.rb) class Book < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :book_comments, dependent: :destroy has_many :favorited_users, through: :favorites, source: :user #いいね数で順番を変えるための validates :title, presence: true validates :body, presence: true, length: { maximum: 200 } # いいね機能 def favorited_by?(user) favorites.where(user_id: user.id).exists? end # 検索機能 def self.search_for(content, method) if method == 'perfect' Book.where(title: content) elsif method == 'forward' Book.where('title LIKE ?', content+'%') elsif method == 'backward' Book.where('title LIKE ?', '%'+content) else Book.where('title LIKE ?', '%'+content+'%') end end # 投稿日数を表示させる scope :created_today, -> { where(created_at: Time.zone.now.all_day) } scope :created_yesterday, -> { where(created_at: 1.day.ago.all_day) } scope :created_this_week, -> { where(created_at: 6.day.ago.beginning_of_day..Time.zone.now.end_of_day) } scope :created_last_week, -> { where(created_at: 2.week.ago.beginning_of_day..1.week.ago.end_of_day) } end
(_book_index.html.erb) <table class='table table-hover table-inverse'> <thead> <tr> <th></th> <th>Title</th> <th>Opinion</th> <th colspan="3"></th> </tr> </thead> <tbody> <% books.each do |book| %> <tr id="book_<%= book.id %>"> <td><%= link_to user_path(book.user) do %> <%= attachment_image_tag(book.user, :profile_image, :fill, 50, 50, fallback: "no-image-icon.jpg") %> <% end %> </td> <td><%= link_to book.title, book_path(book), class: "book_#{book.id}" %></td> <td><%= book.body %></td> <td class="favorite-btn"><%= render "favorites/favorite-btn", book: book %></td> <td class="comments-count">コメント数: <%= book.book_comments.count %></td> </tr> <% end %> </tbody> </table> <h3>投稿数の前日比・前週比</h3> <div class="table_width"> <table class='table table-bordered'> <thead> <tr> <th>今日の投稿数</th> <th>前日の投稿数</th> <th>前日比</th> </tr> </thead> <tbody> <tr> <td><%= @today_book.count %></td> <td><%= @yesterday_book.count %></td> <td> <% if @yesterday_book.count == 0 %> 前日の投稿が0のため計算できません <% else %> <% @the_day_before = @today_book.count / @yesterday_book.count.to_f %> <%= (@the_day_before * 100).round %>% <% end %> </td> </tr> </tbody> </table> </div> <div class="table_width"> <table class='table table-bordered'> <thead> <tr> <th>今週の投稿数</th> <th>前週の投稿数</th> <th>前週比</th> </tr> </thead> <tbody> <tr> <td><%= @this_week_book.count %></td> <td><%= @last_week_book.count %></td> <td> <% if @last_week_book.count == 0 %> 前週の投稿が0のため計算できません <% else %> <% @the_week_before = @this_week_book.count / @last_week_book.count.to_f %> <%= (@the_week_before * 100).round %>% <% end %> </td> </tr> </tbody> </table> </div>
(show.html.erb) <div class='container px-5 px-sm-0'> <div class='row'> <div class='col-md-3'> <h2>User info</h2> <%= render 'info', user: @user %> <h2 class="mt-3">New book</h2> <%= render 'books/form', book: @book %> </div> <div class='col-md-8 offset-md-1'> <h2>Books</h2> <%= render 'book_index',books: @books %> <!--%= current_user.books.where('created_at > ?', Date.today).count %>--> <!--↑↑詳細ページに本日の投稿数を表示--> </div> </div> </div>

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

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

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

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

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

winterboum

2021/11/10 00:10

プログラムcodeなしでは何も分かりません
winterboum

2021/11/10 00:26

それでは読みにくいので <code>を使うように修正してください。 前後に 半角逆コーテーション3つ ``` だけの行で囲めばそうなります。
winterboum

2021/11/10 00:31

あと なんという file名なのか、も重要な情報なので、それを落とさないで
guest

回答1

0

自己解決

こちらのdbの問題でした。

投稿2021/12/07 05:50

sattaku1538

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問