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

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

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

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

Ruby

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

解決済

1回答

664閲覧

メソッドの追加方法 (今回はdevise gemにある項目に追加したい)

hibinyo1031

総合スコア1

Devise

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

Ruby

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails

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

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

0グッド

0クリップ

投稿2020/12/07 06:38

前提・実現したいこと

deviseのgemにもともとある項目(name)(e-mail)(password)(password確認)に
(code)項目を作たい。(password)(code)で二重に確認をして出勤できるようにしたいから。

ここに質問の内容を詳しく書いてください。
ruby on railsで勤怠システムを作っています。
devise機能に追加したい項目(コード)項目を実装中に以下のエラーメッセージが発生しました。
経緯はdeviseのgemを利用してプラスで本人確認の項目を付けようと考えたからです。

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

NoMethodError in Devise::Registrations#new

エラーメッセージ
Showing /Users/gradius/last_model/kintaikun2/app/views/devise/registrations/new.html.haml where line #34 raised:

undefined method `code_field' for #ActionView::Helpers::FormBuilder:0x00007fecebdb24c0
Did you mean? color_field
date_field
phone_field

該当のソースコード

ruby

view/devise/registrations

.AccountPage
.AccountPage__title
%h2 Create Account
%h5 新規アカウントの作成
= render "devise/shared/links"
.AccountPage__form
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
= devise_error_messages!
.FormField
.FormField__label--normal
= f.label :name
.FormField__input--normal
= f.text_field :name, autofocus: true
.FormField
.FormField__label--normal
= f.label :email
.FormField__input--normal
= f.email_field :email
.FormField
.FormField__label--normal
= f.label :password
%i (英数字8文字以上)
.FormField__input--normal
= f.password_field :password, autocomplete: "off"
.FormField
.FormField__label--normal
= f.label :password_confirmation
.FormField__input--normal
= f.password_field :password_confirmation, autocomplete: "off"
.FormField
.FormField__label--normal
= f.label :code
.FormField__input--normal
= f.code_field :code
.actions
= f.submit "Create Account", class: 'Button UserButton'

db/migrate/devise_create_users.rb

class DeviseCreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
## Database authenticatable
t.string :name, null: false
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.integer :code, null: false
t.references :place, foreign_key: true

## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps null: false end add_index :users, :name, unique: true add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true add_colmun :users, :code, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true

end
end

ここに問題に対して試したことを記載してください。
devise_create_users.rbに
add_column :users, :code, :integerを表記し
ターミナルで rails db:migrateを行った

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

app/controllers/application_controller.rbに

protected def configure_permitted_parameters devise_parameter_sanitizer.permit( :sign_up, keys: [ :code, ] ) end

を記述して下さい。
詳しくは
https://github.com/heartcombo/devise

如何でしょうか?

投稿2020/12/07 08:07

tomtomtomtom

総合スコア563

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

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

hibinyo1031

2020/12/07 10:05

ご指摘ありがとうございます。 上記の方法にさらに、テーブル内に[code]カラムを追加し、 コントローラーのcreateアクションに[private]メソッドの[user_params]の[:user]に追加する内容に [:code]を入れてうまく行きました。 ありがとうございました。
tomtomtomtom

2020/12/07 10:08

Database authenticatableを拝見する限りでは、既にcodeカラムは存在してるかと思ったのですが、、、
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問