渲染名为路线的轨道

发布于 2024-12-08 17:42:39 字数 1818 浏览 0 评论 0 原文

如何从控制器正确渲染名为路线的轨道?

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"}

谢谢

How do I render a rails named route properly from controller?

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

Problem is: the signup page is on /signup and when the data in @user is not filled out properly and render :new is called, instead of going to the url /signup it goes to /users. I would use redirect_to but id prefer not to because I want the errors saved off on the page to tell the users which data was not provided.

Update after added 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"}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小兔几 2024-12-15 17:42:39

还添加此路线:

match "signup" => "users#create", :via => "post"

Add this route also:

match "signup" => "users#create", :via => "post"
独自唱情﹋歌 2024-12-15 17:42:39

routes.rb中,您可以添加

get "signup", to: "users#new"
post "signup", to: "users#create"
put "signup", to: "users#update"

并在注册表表单中 - 检查signup_path

form_for(resource, as: resource_name, url: signup_path, html: {method: 'post'})

对于使用过其他读者 >devise_for :users,可以将路由定义为:

devise_scope :user do
  get "signup", to: "devise/registrations#new"
  post "signup", to: "devise/registrations#create"
  put "signup", to: "devise/registrations#update"
end

和注册表单如上。

通过添加此路由,即使用户填写注册表单时出现一些错误,您也可以使用命名路由,例如(注册,注册)。

In routes.rb You can add

get "signup", to: "users#new"
post "signup", to: "users#create"
put "signup", to: "users#update"

And in Registration form - Check signup_path

form_for(resource, as: resource_name, url: signup_path, html: {method: 'post'})

For Other Readers who has used devise_for :users, can defined routes as:

devise_scope :user do
  get "signup", to: "devise/registrations#new"
  post "signup", to: "devise/registrations#create"
  put "signup", to: "devise/registrations#update"
end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文