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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Ruby on Rails

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

Q&A

解決済

1回答

1686閲覧

railsでformを送信したいところに送信できない

kobaryo04ysh

総合スコア29

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

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

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/28 13:59

編集2020/02/28 14:43

前提・実現したいこと

deviseで実装した、ユーザーの編集フォームでユーザーを編集したい。

edit.html.erbで入力したnameの値をusersテーブルのnameカラムに保存したい。

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

deviseにてログイン機能を実装したのちに、ユーザー詳細ページを実装、そこからユーザーの名前、メールアドレス、パスワードを変更できるフォームを作成(viewファイルは元々gemをインストールした際にあら鍵目あったものを使用)しました。

そのフォームでは、email、passwordは変更ができるのですが、nameのみ変更できませんでした。

そこで、rails consoleにてUser.allで調べてみると、nameがnilになっていました。

データベースのテーブルを間違えているかもしれないです。

該当のソースコード

以下にschema.rbのコードを表示します。
今回、フォームを送信したら、usersテーブルのnameに反映されるようにしたかったのですが、できませんでした。
userを新規登録する際にnameのフォームに入力された値はprofileのnameに保存されているようで、erbファイルでcurrent_user.profile.nameとすると表示されます。しかし、current_user.nameではもちろんnilですので表示されません。

schema

1# This file is auto-generated from the current state of the database. Instead 2# of editing this file, please use the migrations feature of Active Record to 3# incrementally modify your database, and then regenerate this schema definition. 4# 5# Note that this schema.rb definition is the authoritative source for your 6# database schema. If you need to create the application database on another 7# system, you should be using db:schema:load, not running all the migrations 8# from scratch. The latter is a flawed and unsustainable approach (the more migrations 9# you'll amass, the slower it'll run and the greater likelihood for issues). 10# 11# It's strongly recommended that you check this file into your version control system. 12 13ActiveRecord::Schema.define(version: 2020_02_27_160636) do 14 15 create_table "comments", force: :cascade do |t| 16 t.integer "post_id" 17 t.string "name" 18 t.text "comment", null: false 19 t.datetime "created_at", null: false 20 t.datetime "updated_at", null: false 21 t.index ["post_id"], name: "index_comments_on_post_id" 22 end 23 24 create_table "posts", force: :cascade do |t| 25 t.string "name" 26 t.string "title" 27 t.text "body" 28 t.datetime "created_at", null: false 29 t.datetime "updated_at", null: false 30 t.integer "user_id" 31 t.string "image_id" 32 t.index ["user_id"], name: "index_posts_on_user_id" 33 end 34 35 create_table "profiles", force: :cascade do |t| 36 t.integer "user_id" 37 t.string "name", default: "", null: false 38 t.datetime "created_at", null: false 39 t.datetime "updated_at", null: false 40 t.index ["user_id"], name: "index_profiles_on_user_id" 41 end 42 43 create_table "users", force: :cascade do |t| 44 t.string "email", default: "", null: false 45 t.string "encrypted_password", default: "", null: false 46 t.string "reset_password_token" 47 t.datetime "reset_password_sent_at" 48 t.datetime "remember_created_at" 49 t.datetime "created_at", null: false 50 t.datetime "updated_at", null: false 51 t.string "name" 52 t.text "profile" 53 t.index ["email"], name: "index_users_on_email", unique: true 54 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 55 end 56 57end 58

user.rb

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 has_many :posts, dependent: :destroy 7 8 has_one :profile, dependent: :destroy 9 10 include Gravtastic 11 gravtastic 12 13 validates :name, presence: true, length: { maximum: 20 } 14 validates :profile, length: { maximum: 200 } 15 validates :password, presence: true, length: { minimum: 6 }, allow_nil: true 16end

profile

1class Profile < ApplicationRecord 2 belongs_to :user 3end

users

1class UsersController < ApplicationController 2 3 def show 4 @user = User.find(params[:id]) 5 end 6 7# def new 8# end 9 10def edit 11 @user = User.find(params[:id]) 12end 13 14def update 15 @user = User.find(params[:id]) 16 17 if @user.update(user_params) 18 flash[:success] = "プロフィールを更新しました" 19 redirect_to user_path 20 else 21 render user_edit_path 22 end 23end 24 25 private 26 27 def user_params 28 params.require(:user).permit(:name, :email, :password,:password_confirmation) 29 end 30end

registrations

1class Users::RegistrationsController < Devise::RegistrationsController 2 # before_action :configure_sign_up_params, only: [:create] 3 # before_action :configure_account_update_params, only: [:update] 4 5 def show 6 @user = User.find(params[:id]) 7 end 8 9 def new 10 end 11 12 def create 13 super 14 #ユーザーが無事に作られていたらprofileを作成 15 if resource 16 #resource==user 17 profile = Profile.new 18 profile.user_id = resource.id 19 profile.name = params[:profile][:name] 20 profile.save 21 end 22 end 23 24 def edit 25 @user = User.find(params[:id]) 26 end

routes

1Rails.application.routes.draw do 2 3 root 'posts#index' 4 5 devise_for :users, :controllers => { 6 :registrations => 'users/registrations', 7 :sessions => 'users/sessions' 8 } 9 10 devise_scope :user do 11 get 'users/:id/edit' => 'users/registrations#edit', as: :user_edit 12 end 13 14 resources :posts 15 resources :comments, only: %i[create destroy] 16 resources :testsessions, only: :create 17 resources :users, only: %i[show update edit] 18end

データベースがぐちゃぐちゃなのは承知です。

少し長くなりましたが、回答お待ちしております。

よろしくお願い致します。

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

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

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

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

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

winterboum

2020/02/28 14:32

コントローラーのcodeを載せてください
kobaryo04ysh

2020/03/01 05:41

コードを追記しました。 回答、お待ちしております!!
guest

回答1

0

ベストアンサー

devise のコントローラーは、セキュリティの都合上、デフォルトだと email や password など一部の情報しか受け取らないようになっています。
そのため、「name は安全なやつだよ」ということを devise に教えてあげなければいけません。

具体的には、 ApplicationController に以下のコードを追加すればOKです。

rb

1class ApplicationController < ActionController::Base 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 4 protected 5 6 def configure_permitted_parameters 7 # 「登録時(sign_up)」に許可するパラメータを追加 8 devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) 9 10 # 「更新時(account_update)」に許可するパラメータを追加 11 devise_parameter_sanitizer.permit(:account_update, keys: [:name]) 12 end 13end

--

ちなみに、上記の回答は github の README を参考に書いています。
これ以外にも色々役立つ情報があるので、一度目を通すと今後の開発が捗るかと思います????

Strong Parameters - heartcombo/devise | github

投稿2020/03/05 03:39

shinoharat

総合スコア1676

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

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

kobaryo04ysh

2020/03/08 18:05

無事に解決できました!ありがとうございます!!日本語の情報のみ追いかけていて、解決が出来なかったので、これからこのような公式のドキュメント含め、どんどん英語の情報にも触れるようにします!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問