如何从控制器正确渲染名为 route 的导轨?
路线.rb:
get "logout" => "sessions#destroy", :as => "logout"
get "login" => "sessions#new", :as => "login"
get "signup" => "users#new", :as => "signup"
root :to => "home#index"
resources :users
resources :sessions
resources :likes
用户控制器.rb:
def new
@user = User.new
end
def create
@user = User.new params[:user]
if @user.save
login(params[:user][:email], params[:user][:password])
redirect_to root_url, :notice => "Welcome! You have signed up successfully."
else
render :new
end
end
问题是:注册页面打开/signup
,当数据@user
没有正确填写render :new
并被调用时,而不是去/signup
它去的 url /users
。我会使用redirect_to
但 id 不喜欢,因为我希望将错误保存在页面上以告诉用户哪些数据未提供。
添加后更新match "signup" => "users#create", :via => "post"
root / {:controller=>"home", :action=>"index"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
signup GET /signup(.:format) {:action=>"new", :controller=>"users"}
POST /signup(.:format) {:action=>"create", :controller=>"users"}
谢谢