前提・実現したいこと
Rails側へaxiosのポストメソッドでデータをdevise側へポストした際に
registrationというつけていない?はずの謎のパラメーターがつくのでそのパラメーターがなぜつくのかを知りたいです
※最終的にvueのページからポストしたパラメーターでユーザーを作成しようとしています
この情報が足りないわ!!ってのがありましたらぜひご教示頂けたら幸いです。
発生している問題・エラーメッセージ
意図しているパラメーター
{ "name"=>"test", "email"=>"test@example.com", "password"=>"password", "password_confirmation"=>"password", "controller"=>"api/v1/auth/registrations", "action"=>"create" }
意図していないパラメーター(実際にRailsのログで確認できるパラメーター)
registrationという謎のパラメーターがつく
{ "name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass", "controller"=>"api/v1/auth/registrations", "action"=>"create", "registration"=>{"name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass"} }
送信した時に出るエラー
back_1 | Started POST "/api/v1/auth/" for 172.18.0.1 at 2021-08-05 08:05:06 +0900 back_1 | Processing by Api::V1::Auth::RegistrationsController#create as HTML back_1 | Parameters: {"name"=>"test", "password"=>"[FILTERED]", "email"=>"test@example.com", "password_confirmation"=>"[FILTERED]", "registration"=>{"name"=>"test", "password"=>"[FILTERED]", "email"=>"test@example.com", "password_confirmation"=>"[FILTERED]"}} back_1 | {"name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass", "controller"=>"api/v1/auth/registrations", "action"=>"create", "registration"=>{"name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass"}} back_1 | Unpermitted parameter: :registration back_1 | {"name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass", "controller"=>"api/v1/auth/registrations", "action"=>"create", "registration"=>{"name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass"}} back_1 | Unpermitted parameter: :registration back_1 | {"name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass", "controller"=>"api/v1/auth/registrations", "action"=>"create", "registration"=>{"name"=>"test", "password"=>"pass", "email"=>"test@example.com", "password_confirmation"=>"pass"}} back_1 | Unpermitted parameter: :registration back_1 | TRANSACTION (0.3ms) BEGIN back_1 | User Exists? (1.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'test@example.com' AND `users`.`provider` = 'email' LIMIT 1 back_1 | TRANSACTION (0.2ms) ROLLBACK back_1 | Completed 422 Unprocessable Entity in 252ms (Views: 0.2ms | ActiveRecord: 2.8ms | Allocations: 14477)
該当のソースコード
POST送信ページ
vue
1<template> 2 <v-container> 3 <v-card width="400px" class="mx-auto mt-5"> 4 <v-card-title> 5 <h1 class="display-1"> 6 新規登録 7 </h1> 8 </v-card-title> 9 <v-card-text> 10 <v-form ref="form" lazy-validation> 11 <v-text-field 12 v-model="user.email" 13 prepend-icon="mdi-email" 14 label="メールアドレス" 15 /> 16 <v-text-field 17 v-model="user.password" 18 prepend-icon="mdi-lock" 19 append-icon="mdi-eye-off" 20 label="パスワード" 21 /> 22 <v-text-field 23 v-model="user.password_confirmation" 24 prepend-icon="mdi-lock" 25 append-icon="mdi-eye-off" 26 label="パスワード確認" 27 /> 28 <v-card-actions> 29 <v-btn 30 color="light-green darken-1" 31 class="white--text" 32 @click="registerUser" 33 > 34 新規登録 35 </v-btn> 36 </v-card-actions> 37 </v-form> 38 </v-card-text> 39 </v-card> 40 </v-container> 41</template> 42 43<script> 44export default { 45 name: "App", 46 auth: false, 47 data() { 48 return { 49 // なんかポストの仕方が悪い? 50 user: { 51 name: "test", 52 password: "", 53 email: "", 54 password_confirmation: "" 55 } 56 }; 57 }, 58 methods: { 59 registerUser() { 60 this.$axios.post("/api/v1/auth/", this.user).then(response => { 61 window.location.href = "/users/comfirmation"; 62 }); 63 } 64 } 65}; 66</script> 67
Rails側
route.rb
rb
1Rails.application.routes.draw do 2 devise_for :users 3 4 namespace :api do 5 namespace :v1 do 6 resources :test, only: %i[index] 7 8 mount_devise_token_auth_for 'User', at: 'auth', controllers: { 9 registrations: 'api/v1/auth/registrations' 10 } 11 12 namespace :auth do 13 resources :sessions, only: %i[index] 14 end 15 end 16 end 17 resources :workspaces, only: [:create, :delete, :edit] 18 resources :users, only: [:create, :delete, :edit] 19 resources :posts, only: [:create, :delete, :edit] 20 resources :houseworks, only: [:create, :delete, :edit] 21 22end 23
registrations_controller.rb
rb
1# アカウント作成用コントローラー 2class Api::V1::Auth::RegistrationsController < DeviseTokenAuth::RegistrationsController 3 private 4 5 def sign_up_params 6 logger.debug(params) 7 params.permit(:email, :password, :password_confirmation,:name) 8 end 9end
試したこと
curlコマンドでPOSTできるか試した
curl -X POST http://localhost:3000/api/v1/auth -d '[name]=example&[email]=example@example.com&[password]=password&[password_confirmation]=password'
back_1 | Started POST "/api/v1/auth" for 172.18.0.1 at 2021-08-05 08:25:19 +0900 back_1 | Processing by Api::V1::Auth::RegistrationsController#create as */* back_1 | Parameters: {"name"=>"example", "email"=>"example@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"} back_1 | {"name"=>"example", "email"=>"example@example.com", "password"=>"password", "password_confirmation"=>"password", "controller"=>"api/v1/auth/registrations", "action"=>"create"} back_1 | {"name"=>"example", "email"=>"example@example.com", "password"=>"password", "password_confirmation"=>"password", "controller"=>"api/v1/auth/registrations", "action"=>"create"} back_1 | {"name"=>"example", "email"=>"example@example.com", "password"=>"password", "password_confirmation"=>"password", "controller"=>"api/v1/auth/registrations", "action"=>"create"} back_1 | TRANSACTION (0.2ms) BEGIN back_1 | User Exists? (0.7ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'example@example.com' AND `users`.`provider` = 'email' LIMIT 1 back_1 | User Create (1.5ms) INSERT INTO `users` (`uid`, `encrypted_password`, `name`, `email`, `created_at`, `updated_at`) VALUES ('example@example.com', '$2a$12$v0GFHKD3QFyVHGUxfcIqueV6ralGVgtQpw20EF4H0YiNp.L/6lXHC', 'example', 'example@example.com', '2021-08-04 23:25:19.304233', '2021-08-04 23:25:19.304233') back_1 | TRANSACTION (6.1ms) COMMIT back_1 | TRANSACTION (0.3ms) BEGIN back_1 | User Update (1.9ms) UPDATE `users` SET `users`.`tokens` = '{\"khZQ6dDU5Qx9TLF0pdwk4w\":{\"token\":\"$2a$10$YYPKCXTOi.Fy1U.Hkmz0QuPyUcLG7Sztn0/o1lnGwICF0PmD1pZb2\",\"expiry\":1629329119}}', `users`.`updated_at` = '2021-08-04 23:25:19.374094' WHERE `users`.`id` = 7 back_1 | TRANSACTION (1.4ms) COMMIT back_1 | User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 7 LIMIT 1 back_1 | TRANSACTION (0.2ms) BEGIN back_1 | User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 7 LIMIT 1 FOR UPDATE back_1 | User Update (1.4ms) UPDATE `users` SET `users`.`tokens` = '{\"khZQ6dDU5Qx9TLF0pdwk4w\":{\"token\":\"$2a$10$lX/yHs29Rj9/KyxkxOWeSu4uFGFGFfd0ICN9y9BSLr48CfbC3cqmO\",\"expiry\":1629329119,\"last_token\":\"$2a$10$YYPKCXTOi.Fy1U.Hkmz0QuPyUcLG7Sztn0/o1lnGwICF0PmD1pZb2\",\"updated_at\":\"2021-08-04T23:25:19Z\"}}', `users`.`updated_at` = '2021-08-04 23:25:19.444964' WHERE `users`.`id` = 7 back_1 | TRANSACTION (1.4ms) COMMIT back_1 | Completed 200 OK in 417ms (Views: 0.1ms | ActiveRecord: 16.4ms | Allocations: 11981)
大丈夫そう
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/05 00:10
2021/08/05 12:09