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
Develop #1
base: main
Are you sure you want to change the base?
Develop #1
Conversation
app/controllers/users_controller.rb
Outdated
def edit | ||
@user = User.find(params[:id]) | ||
end | ||
|
||
def update | ||
@user = User.find(params[:id]) | ||
|
||
if @user.update(user_params) |
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.
User.find
だとURLのidを書き換えたら他の人の情報を編集できてしまいます。この書き方は初心者にほんとによくある書き方なので気をつけましょう。
@user = current_user # または User.find(current_user.id)
にしないとですね。
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/controllers/users_controller.rb
Outdated
private | ||
|
||
def user_params | ||
params.require(:user).permit(:name, :email, :admin, :password, :password_confirmation, :image) |
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.
:admin
を許可するのはまずいです。なぜでしょう?
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.
変更を可能にしてしまうからですね!変更します!
@question = current_user.questions.find(params[:id]) | ||
@question.update!(question_params) | ||
redirect_to "/questions/#{@question.id}", notice: "タスク『#{@question.title}』を更新しました" |
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.
失敗時の考慮もしないとですね。
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.
変更します!
@question = current_user.questions.new(question_params) | ||
|
||
if @question.save | ||
redirect_to "/questions/#{@question.id}", notice: "タスク『#{@question.title}』を登録しました" |
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.
redirect_to @question
またはredirect_to question_path(@question)
の方がRailsっぽくてよいです。
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.
変更します!統一感もありますもんね!
<% if @answer.errors.present? %> | ||
<ul class="alert alert-danger" role="alert"> | ||
<% @answer.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
<% 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.
エラーメッセージの表示についてはいろいろなところで使い回すと思うのでパーシャルにしといたほうが良いと思いますよー。
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.
他エラーのパーシャルもあって、showでのみ使うエラー文だったため、パーシャル化しなかったのですが、やっぱりパーシャルのが見やすいですよね!変更します!
<%= form_with scope: :session, local: true do |f| %> | ||
<div class="form-group"> | ||
<%= f.label :email %> | ||
<%= f.text_field :email, class: 'form-control', id: 'sessions_email' %> |
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.
email_field
を使いましょう。
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.
変更します。気づきませんでした。
def index | ||
@q = Question.ransack(params[:q]) | ||
@questions = @q.result(distinct: true).page(params[:page]) | ||
@search_path = '/questions' |
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.
questions_path
のほうが良いですね。
基本的に文字列を直接書くのはプログラミング全般において推奨されてません。
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.
変更します!
<li class="media mb-3 border-bottom pb-3"> | ||
<div class="d-flex gap-3"> | ||
<div class="flex-shrink-0"> | ||
<% if question.user.image.attached? %> |
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.
N+1問題起きてるのでぜひ確認 & 解消してみてください。
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.
挑戦します!
@@ -5,7 +5,7 @@ def create | |||
redirect_to admin_question_path(params[:question_id]), notice: '解答しました' | |||
else | |||
@question = Question.find(params[:question_id]) | |||
render 'admin/questions/show' | |||
render admin_question_path(@question) |
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.
renderとredirectをちゃんと理解できてない気がします。
たぶんこれ動かない気がするんですが、動作試してもらえますか?
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.
戻すの忘れてました。
一般の方でエラーが出て、調べてもpathでの書き方が見つからなく、
rails ガイドをみると、'admin/questions/show'こういう文字列になっていたので、
そちらに合わせていたのですが、、、、
変更しました!
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.
これではがっつりエラー出てました!
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.
一般の方でエラーが出て、調べてもpathでの書き方が見つからなく、
ここの文面的に_path
が何を返す関数なのかを理解してなさそうな気がしました。_path
ってどんな返り値でしょう?
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.
urlの扱いやすい版みたいな感じで考えていました。コンソールで調べると、
pry(main)> app.users_path
=> "/users"
となっているので、urlの文字列が返り値です!
ネットでもう一度調べると、
pathは相対ルートパスとも書いていて、やっぱりurl を扱いやすくしたものなのかと思いました。
自分がいまいち理解してなかったのは、レンダーでした。
render :new
の場合、レンダーは引数にハッシュを指定する。この場合、キー(template)に、値(erbファイルをさす文字列)
なので、erbファイルの位置を指定が必要で、urlを指定してもだめだったのですね!
合ってますでしょうか!?。。。
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.
概ねあってます!
urlを扱いやすくしたもの
という表現はちょいと気になりますね。
URL, ホスト, ドメイン, パス, クエリなどを調べてみてください。その中のパスが_path
に該当します。
参考
https://developer.mozilla.org/ja/docs/Learn/Common_questions/What_is_a_URL
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.
確認しました。
urlのパスの部分を扱いやすくしたものが正解ですね!ざっくりしすぎてました。
app/controllers/users_controller.rb
Outdated
@@ -22,11 +22,11 @@ def create | |||
end | |||
|
|||
def edit | |||
@user = User.find(params[:id]) | |||
@user = current_user.find(params[:id]) |
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.
ここもちょいと違います。たぶん動かないと思います。
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.
動きませんでした!editについてあまり意識していなかったので、チェックし忘れてました。。。
変更します!
app/controllers/users_controller.rb
Outdated
end | ||
|
||
def update | ||
@user = User.find(params[:id]) | ||
@user = current_user.find(params[:id]) |
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.
同様にここも動かないと思います。
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.
変更しました!
@@ -0,0 +1,7 @@ | |||
<% if answer.errors.present? %> |
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.
answerに特化しすぎなので object.errors.present?
とかに命名を変更したほうが良くないでしょうか?
で、ファイルの置き場はviews/shared
配下に置く感じですね。
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.
すいません。その場合、renderの変数部分は
<%= render partial: 'shared/alert',locals: {object: @object } %>
こんな感じになるかと思うのですが、answerモデルにどう紐付けするのでしょう!?
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.
この質問で解決した認識で良いですかね?
https://tech-essentials.work/courses/7/tasks/54/outputs/1450
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/views/questions/show.html.erb
Outdated
@@ -28,15 +28,15 @@ | |||
<div class="badge bg-success text-wrap">解決済み</div> | |||
<% else %> | |||
<div class="badge bg-danger text-wrap">未解決</div> | |||
<% if session[:user_id].present? && question.user_id == current_user.id %> | |||
<% if session[:user_id].present? && @question.user_id == current_user.id %> |
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.
コントローラにログインしてるかどうかを返すメソッドを作成してそれを使うように変えてみると勉強になると思います。
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.
やってみます!
user_loginedみたいなヘルパーメソッドを作成してみます!!
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/controllers/users_controller.rb
Outdated
u = User.find(params[:id]) | ||
if current_user == u | ||
@user = u | ||
elsif | ||
flash[:danger] = 'アカウントの編集権限がありません' | ||
redirect_to questions_path | ||
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.
ちょっと冗長ですね。
@user = User.find(params[:id])
if current_user != @user
flash[:danger] = 'アカウントの編集権限がありません'
redirect_to questions_path
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.
なるほど。ワンクションあっていいなーと思ったのですが、勉強なります!変更します!
なかなか時間がかかってしまいましたが、たくさんの事勉強になりました。
すいません。コミットがざっくりしてます。
本当は一箇所変更するたびにコミットすると考えると、なかなか面倒だなーと思わされました。
すごく見にくいと思います。よろしくお願いします。