質問編集履歴

1

modelの追加

2022/02/24 12:42

投稿

takoyan
takoyan

スコア105

test CHANGED
File without changes
test CHANGED
@@ -84,25 +84,6 @@
84
84
  ```Ruby
85
85
  #commnet.rb
86
86
 
87
- # == Schema Information
88
- #
89
- # Table name: comments
90
- #
91
- # id :integer not null, primary key
92
- # comment :text(65535) not null
93
- # name :string(255) not null
94
- # created_at :datetime not null
95
- # updated_at :datetime not null
96
- # board_id :integer
97
- #
98
- # Indexes
99
- #
100
- # index_comments_on_board_id (board_id)
101
- #
102
- # Foreign Keys
103
- #
104
- # fk_rails_... (board_id => boards.id)
105
- #
106
87
  class Comment < ApplicationRecord
107
88
  belongs_to :board
108
89
  has_many :comments, dependent: :destroy
@@ -116,30 +97,102 @@
116
97
  ```Ruby
117
98
  #user.rb
118
99
 
119
- # == Schema Information
120
- #
121
- # Table name: users
122
- #
123
- # id :integer not null, primary key
124
- # birthday :date
125
- # faculty :string(255)
126
- # name :string(255) not null
127
- # password_digest :string(255) not null
128
- # profile :string(255)
129
- # created_at :datetime not null
130
- # updated_at :datetime not null
131
- #
132
- # Indexes
133
- #
134
- # index_users_on_name (name) UNIQUE
135
- #
136
100
  class User < ApplicationRecord
137
101
  has_secure_password
138
102
  has_many :boards, dependent: :destroy
139
103
  has_many :comments, dependent: :destroy
104
+
105
+ validates :name,
106
+ presence: true,
107
+ uniqueness: true,
108
+ length: {maximum:16}
109
+ # format: {
110
+ # with: /\A[a-z0-9]+\z/,
111
+ # message: 'は小文字英数字で入力してください'
140
- #省略
112
+ # }
113
+ validates :password,
114
+ length: {minimum: 8 }
115
+
116
+ def age
117
+ now = Time.zone.now
118
+ (now.strftime('%Y%m%d').to_i - birthday.strftime('%Y%m%d').to_i) / 10000
119
+ end
141
120
  end
142
121
  ```
122
+ ```Ruby
123
+ #board.rb
124
+ class Board < ApplicationRecord
125
+ has_many :comments, dependent: :delete_all
126
+ has_many :board_tag_relations, dependent: :delete_all
127
+ has_many :tags, through: :board_tag_relations
128
+ has_one_attached :image
129
+ belongs_to :user
130
+
131
+ validates :name, presence: true, length: { maximum: 10 }
132
+ validates :title, presence: true, length: { maximum: 30 }
133
+ validates :body, presence: true, length: { maximum: 1000 }
134
+
135
+ scope :created_today, -> { where(created_at: Time.zone.now.all_day) } # 今日
136
+ scope :created_last_month, -> { where(created_at: Time.zone.now.prev_month.all_day) } # 1ヶ月前の投稿
137
+ scope :created_month, -> { where(created_at: Time.zone.now.all_month) } # 今月の投稿
138
+
139
+ validates :image, content_type: { in: %w[image/jpeg image/gif image/png],
140
+ message: "must be a valid image format" },
141
+ size: { less_than: 5.megabytes,
142
+ message: "should be less than 5MB" }
143
+
144
+ # 表示用のリサイズ済み画像を返す
145
+ def display_image
146
+ image.variant(resize_to_limit: [500, 500])
147
+ end
148
+ end
149
+ ```
150
+
151
+ ```Ruby
152
+ #UsersController.rb
153
+ class UsersController < ApplicationController
154
+ def new
155
+ @user = User.new(flash[:user])
156
+ end
157
+
158
+ def create
159
+ user = User.new(user_params)
160
+ if user.save
161
+ session[:user_id] = user.id
162
+ redirect_to mypage_path
163
+ else
164
+ flash[:user] = user
165
+ flash[:error_messages] = user.errors.full_messages
166
+ redirect_back fallback_location: 'http://localhost'
167
+ end
168
+ end
169
+
170
+ def me
171
+ end
172
+
173
+ def edit
174
+ @user = @current_user
175
+ end
176
+
177
+ def update
178
+ user = @current_user
179
+ if user.update(user_params)
180
+ session[:user_id] = user.id
181
+ redirect_to mypage_path
182
+ else
183
+ flash[:error_messages] = user.errors.full_messages
184
+ end
185
+ end
186
+
187
+ private
188
+
189
+ def user_params
190
+ params.require(:user).permit(:name, :password, :password_confirmation, :faculty, :profile)
191
+ end
192
+ end
193
+ ```
194
+
195
+
143
196
 
144
197
  参考記事
145
198
  https://qiita.com/japwork/items/b2a7e61379c88a5e60c8