渲染名为路线的轨道
如何从控制器正确渲染名为路线的轨道?
routes.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
user_controller.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
被调用,而不是转到 url /signup
,而是转到 /users
。我会使用 redirect_to
但 id 不喜欢这样做,因为我希望将错误保存在页面上以告诉用户未提供哪些数据。
添加 match "signup" => 后更新“用户#创建”,:via => “帖子”
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"}
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还添加此路线:
Add this route also:
在routes.rb中,您可以添加
并在注册表表单中 - 检查signup_path
对于使用过其他读者 >devise_for :users,可以将路由定义为:
和注册表单如上。
通过添加此路由,即使用户填写注册表单时出现一些错误,您也可以使用命名路由,例如(注册,注册)。
In routes.rb You can add
And in Registration form - Check signup_path
For Other Readers who has used devise_for :users, can defined routes as:
And Registration form as above.
By adding this routes, you can use your named routes like (register, signup) even after user fills register form with some error.