在為 /signup 路由運行 post 方法時,我無法注冊出現錯誤的用戶。
請幫忙 !!!
這是我的路線.rb
Rails.application.routes.draw do
get 'signup' => 'users#signup'
post 'signup' => 'user#create'
root 'home#index'
get 'edit' => 'home#edit'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :profiles , only: [:update]
resources :educations , only: [:new]
end
這是用戶控制器
class UsersController < ApplicationController
def signup
end
def create
user = User.find_by(email: params[:user][:email].downcase)
if user && user.authenticate(params[:user][:password])
log_in(user)
redirect_to(root_path)
else
flash.now[:danger] = 'Invalid email/password combination'
render('new')
end
end
end
這是我在視圖下的用戶檔案夾下的 html gile
<div class="w-screen h-screen flex justify-center items-center">
<div class="flex flex-col w-full max-w-md px-4 py-8 bg-white rounded-lg shadow dark:bg-gray-800 sm:px-6 md:px-8 lg:px-10 m-10">
<div class="self-center mb-6 text-xl font-light text-gray-600 sm:text-2xl dark:text-white">
Create A New Account
</div>
<div class="flex items-center justify-center mt-6">
<%= link_to "Already have an account? Log in", "/login", class: 'inline-flex items-center text-md font-thin text-center text-gray-500 hover:text-gray-700 dark:text-gray-100 dark:hover:text-white' %></p>
</div>
<div class="mt-8">
<%= form_for(:user, url: signup_path) do |f| %>
<% login_path %>
<div class="flex flex-col mb-2">
<div class="flex relative ">
<%= f.text_field :name, value: 'Aditya', class: 'rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent', placeholder: 'Your name' %>
</div>
</div>
<div class="flex flex-col mb-2">
<div class="flex relative ">
<%= f.email_field :email, value: '[email protected]', class: 'rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent', placeholder: 'Your email' %>
</div>
</div>
<div class="flex flex-col mb-2">
<div class="flex relative ">
<%= f.password_field :password, value: 'password', class: 'rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent', placeholder: 'New Password' %>
</div>
</div>
<div class="flex flex-col mb-2">
<div class="flex relative ">
<%= f.password_field :cpassword, value: 'password', class: 'rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-white text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent', placeholder: 'Confirm Password' %>
</div>
</div>
<div class="flex w-full">
<%= f.submit "Sign Up", class: "py-2 px-4 bg-purple-600 hover:bg-purple-700 focus:ring-purple-500 focus:ring-offset-purple-200 text-white w-full transition ease-in duration-200 text-center text-base font-semibold shadow-md focus:outline-none focus:ring-2 focus:ring-offset-2 rounded-lg" %>
</div>
<% end %>
</div>
</div>
</div>
提交表格時:
得到錯誤:
在 2022-01-11 00:14:04 0530 為 ::1 開始 POST "/signup"
ActionController::RoutingError (uninitialized constant UserController
Did you mean? UsersController):
uj5u.com熱心網友回復:
在您的routes.rbfrom 中更改以下行
post 'signup' => 'user#create'
到
post 'signup' => 'users#create'
因為你的控制器被命名UsersController(注意復數)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407788.html
標籤:
