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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Q&A

解決済

1回答

1350閲覧

ユーザー情報の更新をしたい

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

0グッド

0クリップ

投稿2020/10/05 05:05

編集2020/10/16 03:14

実現したいこと

更新ボタンを押すと編集した内容が反映され、トップページへ飛ぶ。

発生している問題・エラーメッセージ

更新ボタンを押すとトップページへ飛べるが反映がされていない

該当のソースコード

関連ファイル
user.rb

erb

1 devise :database_authenticatable, :registerable, 2 :recoverable, :rememberable, :validatable 3 validates :password, length: { minimum: 6 }, format: { with: /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]+\z/i } 4 with_options presence: true do 5 validates :email, uniqueness: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})\z/i } 6 validates :nickname 7 end 8 has_many :books 9 mount_uploader :image, ImageUploader 10end 11

users_controller.rb

erb

1before_action :authenticate_user!, only: [:edit] 2 def show 3 @user = User.find(params[:id]) 4 @books = @user.books 5 end 6 def edit 7 @user = User.find(params[:id]) 8 end 9 10 def update 11 @user = User.find(params[:id]) 12 if @user.id == current_user.id 13 @user.update(user_params) 14 redirect_to root_path 15 else 16 render 'edit' 17 end 18 end 19 20private 21 #ストロングパラメーター 22 def user_params 23 params.require(:user).permit(:nickname, :image ) 24 end 25end 26

users/edit.html.erb

erb

1 <%= form_for(@user) do |f| %> 2<div class="field"> 3 <%= f.label "アイコン画像" %><br/> 4 <%= f.file_field :image %> 5</div> 6<%= f.label "ニックネーム"%> 7<%= f.text_field :nickname%> 8<%= f.submit '送信'%> 9<% end %> 10

試したこと

様々な記事を参考にしましたがわからなかったので、お力を貸していただけたらと思いました

補足情報(FW/ツールのバージョンなど)

ruby 2.6.5
rails 6.0.0

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

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

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

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

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

hatsu

2020/10/17 00:58

更新ボタンを押してからトップページに飛ぶまでのログとかありますでしょうか?貼っていただけたらヒントになるかもです!
退会済みユーザー

退会済みユーザー

2020/10/17 01:28

Started PATCH "/users/1" for ::1 at 2020-10-17 10:25:40 +0900 Processing by UsersController#update as HTML Parameters: {"authenticity_token"=>"YhxXdXOY5DYvNnNUirWsuIefy+ZOK5pAHg1iyRjcRCckM54SeIuvGV/BU/Xt2hnfbNC0abyd1GJSkBUT8tHk9w==", "user"=>{"nickname"=>"テストくん"}, "commit"=>"送信", "id"=>"1"} User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 ↳ app/controllers/users_controller.rb:6:in `update' User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/users_controller.rb:7:in `update' DEPRECATION WARNING: Uniqueness validator will no longer enforce case sensitive comparison in Rails 6.1. To continue case sensitive comparison on the :email attribute in User model, pass `case_sensitive: true` option explicitly to the uniqueness validator. (called from update at /Users/hayashi/book_app/app/controllers/users_controller.rb:8) (0.2ms) BEGIN ↳ app/controllers/users_controller.rb:8:in `update' User Exists? (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'ccc@ccc.com' AND `users`.`id` != 1 LIMIT 1 ↳ app/controllers/users_controller.rb:8:in `update' (0.2ms) ROLLBACK ↳ app/controllers/users_controller.rb:8:in `update' Redirected to http://localhost:3000/ Completed 302 Found in 12ms (ActiveRecord: 1.5ms | Allocations: 6813) これです
hatsu

2020/10/17 01:59

ここのSQLでROLLBACKが走っていますね ``` SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'ccc@ccc.com' AND `users`.`id` != 1 LIMIT 1 ``` current_userを探している感じでしょうか。 別のControllerとかでログインしていないとトップページに飛ばすような記述があったりしますか? あとupdateのログがないような気がしています。 @user.update(user_params)の前でbinding.pryしてとまれば正常にupdateアクションに届いていると判定できそうですが。
退会済みユーザー

退会済みユーザー

2020/10/17 02:36

updateアクションはbinding.pryで止まるので動いていると思います controllerもトップページに飛ぶような記述はしていないと思いますがどうでしょうか class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :configure_permitted_parameters, if: :devise_controller? before_action :search_book def search_book @p = Book.ransack(params[:q]) @results = @p.result end protected def configure_permitted_parameters user_dates = %i[nickname] devise_parameter_sanitizer.permit :sign_up, keys: user_dates end end class LikesController < ApplicationController before_action :authenticate_user! def create @book = Book.find(params[:book_id]) like = current_user.likes.new(book_id: @book.id) like.save end def destroy @book = Book.find(params[:book_id]) like = current_user.likes.find_by(book_id: @book.id) like.destroy end end class BooksController < ApplicationController before_action :set_book, only: %i[edit show update destroy] def index @books = Book.order('created_at DESC') if params[:tag] @books = Book.tagged_with(params[:tag]) else @books = Book.all end @categorys = Category.where.not(id: 1) end def new @book = Book.new @tags = ActsAsTaggableOn::Tag.all end def destroy if user_signed_in? && current_user.id == @book.user_id if @book.destroy redirect_to root_path else render :show end else redirect_to new_user_session_path end end def show end def edit end def update if @book.update(book_params) redirect_to root_path else render :edit end end def category @books = Book.category(params[:id]) end def create @book = Book.new(book_params) if @book.save redirect_to root_path else render :new end end private def set_book @book = Book.find(params[:id]) end def book_params params.require(:book).permit(:title, :price, :category_id, :tag_list, :review, :author, :image, :description).merge(user_id: current_user.id) end end
guest

回答1

0

ベストアンサー

調べた結果、passwordが無いと更新できないみたいなので、モデルにcreateアクション時のみにvalidateかける
on: :createを書き足したら解決できました

投稿2020/10/18 03:45

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問