rails guide でrenderについて読んでいてrenderとredirect_toの違い、ここの少し下の2.3.2の例をみていて、ここではredirect_toを使うとgetが増えるので、validationなどに失敗したときはrenderしてそこで使われるオブジェクトを読み込んでおけばリクエストが減ると書いてあり、これを使えばほとんどredirect_toを使わなくても良いのではないかと思います。
実験してみたらちゃんと動きました。
rb
1def create 2 user = User.find_by(email: params[:session][:email].downcase) 3 if user && user.authenticate(params[:session][:password]) 4 if user.activated? 5 log_in user 6 params[:session][:remember_me] == '1' ? remember(user) : forget(user) 7 @user = current_user⬅︎@userはshowで@user = User.find(params[:id])としている 8 render "users/show"⬅︎ここをrenderにしている 9 # redirect_to user 10 else 11 message = "アカウントが有効ではありません。" 12 message+= "メールアドレスをご確認ください。" 13 flash[:warning] = message 14 redirect_to root_url 15 end 16 else 17 flash.now[:danger] = "メールアドレスまたはパスワードが間違っています" 18 render 'new' 19 end 20 end
これでリクエストは減りました。
before
Started POST "/login" for ::1 at 2019-11-05 15:46:54 +0900 Processing by SessionsController#create as HTML Parameters: {"authenticity_token"=>"fzWH3PL9hHqclHtfL/rULvhhaQPL58Xn6IFu9pyy4S6rDP93x93swAgepiwWXcwKbrjPqcx0mKYESTjVyp8GiQ==", "session"=>{"email"=>"admin@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"ログイン"} User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "admin@gmail.com"], ["LIMIT", 1]] ↳ app/controllers/sessions_controller.rb:6:in `create' Redirected to http://localhost:3000/users/ca89455c-a092-497f-b3dd-9744f6dd852f Completed 302 Found in 303ms (ActiveRecord: 8.6ms | Allocations: 10387) Started GET "/users/ca89455c-a092-497f-b3dd-9744f6dd852f" for ::1 at 2019-11-05 15:46:54 +0900 Processing by UsersController#show as HTML Parameters: {"id"=>"ca89455c-a092-497f-b3dd-9744f6dd852f"} User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", "ca89455c-a092-497f-b3dd-9744f6dd852f"], ["LIMIT", 1]] ↳ app/controllers/users_controller.rb:25:in `show' Rendering users/show.html.erb within layouts/application Rendered users/show.html.erb within layouts/application (Duration: 1.1ms | Allocations: 265) CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", "ca89455c-a092-497f-b3dd-9744f6dd852f"], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:8:in `current_user' Rendered layouts/_deviation_common.html.erb (Duration: 1.7ms | Allocations: 725) Rendered layouts/_deviation_footer.html.erb (Duration: 0.1ms | Allocations: 71) Completed 200 OK in 18ms (Views: 15.2ms | ActiveRecord: 0.4ms | Allocations: 5620)
after
Started POST "/login" for ::1 at 2019-11-05 16:09:42 +0900 Processing by SessionsController#create as HTML Parameters: {"authenticity_token"=>"HWm7vtmLEFo9D4GJ/oC0h0SvxfJukyF2uJsqgeFSOWrJUMMV7Kt44KmFXPrHJ6yj0nZjWGkAfDdUU3yit3/ezQ==", "session"=>{"email"=>"admin@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"ログイン"} User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "admin@gmail.com"], ["LIMIT", 1]] ↳ app/controllers/sessions_controller.rb:6:in `create' User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", "ca89455c-a092-497f-b3dd-9744f6dd852f"], ["LIMIT", 1]] ↳ app/helpers/sessions_helper.rb:8:in `current_user' Rendering users/show.html.erb within layouts/application Rendered users/show.html.erb within layouts/application (Duration: 0.3ms | Allocations: 91) Rendered layouts/_deviation_common.html.erb (Duration: 0.5ms | Allocations: 162) Rendered layouts/_deviation_footer.html.erb (Duration: 0.1ms | Allocations: 68) Completed 200 OK in 285ms (Views: 12.4ms | ActiveRecord: 0.7ms | Allocations: 5107)
またrailsはどのコントローラにいてもすべてのviewをよみこんでいるのでどこからでもテンプレートやレイアウトrenderできるということでしょうか?ならばredirect_toはあまり使いたくないなと思いました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/05 07:59