【发布时间】:2014-11-07 17:12:16
【问题描述】:
我的 signup_path 中有以下注册表单,映射到 users#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 反对 render "new",但如果我这样做,错误消息就会消失,这是我不想要的。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rails-routing