我在signup_path中有以下注册表单,该表单映射到用户#new
在我的new.html.erb
中 <%= form_for(@user) do |f|%>
<!---handles the error messages within the form. -->
<%= render "shared/error_messages"%>
<%= f.label :name%>
<%= f.text_field :name, class: 'form-control'%></br>
<%= f.label :email%>
<%= f.text_field :email, class: 'form-control'%></br>
<%= f.label :password%>
<%= f.password_field :password, class: 'form-control'%></br>
<%= f.label :password_confirmation, "Confirmation"%>
<%= f.password_field :password_confirmation, class: 'form-control' %></br>
<%= f.submit "Create My Account!", class:"btn btn-theme btn-block"%>
<%end%>
在我的routes.rb
中get "signup" => "users#new"
现在我可以在 localhost:3000 / signup 成功注册新用户,这是理想的选择。但是,每当我尝试通过将部分或全部字段留空来测试表单时,它会将我重定向到 localhost:3000 / users 。我想也许在我的控制器中有些东西是关闭的,但我找不到任何奇怪的东西
user_controller.rb
class UsersController < ApplicationController
# before accessing the only and edit RESTful thing, go to logged_in_user first.
before_action :logged_in_user, only: [:edit, :update, :index, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: [:destroy]
def index
@users = User.all
end
def new
# create a new user!
@user = User.new
end
def show
# declare a user variable, assign it to the current user
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
#before we watned to log the user in after they create their account, now we want them to activate their emails
#log_in @user
#flash[:success] = "Welcome!"
#redirect_to @user
@user.send_activation_email
flash[:info] = "Please check your email to activate your account."
redirect_to login_url
else
render "new"
end
end
我在这里做错了什么?为什么路线会改变?我尝试 redirect_to signup_path 反对渲染“新”,但如果我这样做,那么错误消息就会消失,这是我不想要的。
答案 0 :(得分:1)
现在您的登录表单发布到/users
,因此当您render "new"
显示的网址正确无误时。如果您想要更改该行为,可以在路线中添加post "signup" => "users#create"
,这样您就可以将表单发布到/signup
,当render "new"
create
时,您会显示所需的网址行动。