Ruby on rails Rails-是否可以使用参数重定向到自定义路径?

Ruby on rails Rails-是否可以使用参数重定向到自定义路径?,ruby-on-rails,ruby,ruby-on-rails-4,authentication,authorization,Ruby On Rails,Ruby,Ruby On Rails 4,Authentication,Authorization,所以我正在制定一个授权系统。新的注册页设置为“/signup”。如果注册无效(用户名已被使用,密码太短),我想显示错误。当我这样做的时候: @user = User.create(user_params) if @user.save session[:user_id] = @user.id redirect_to stories_path, notice: "Thanks for registration!" else render

所以我正在制定一个授权系统。新的注册页设置为“/signup”。如果注册无效(用户名已被使用,密码太短),我想显示错误。当我这样做的时候:

@user = User.create(user_params)
    if @user.save
        session[:user_id] = @user.id
        redirect_to stories_path, notice: "Thanks for registration!"
    else
        render 'new'
    end
post 'signup` => 'users#create`
它可以工作,但它以“/users”而不是“/signup”呈现。而不是什么时候

呈现“新”

我写

将_重定向到“/signup”

它会重定向,但不会显示错误。是否可以重定向到“/signup”并保留错误

user.rb:

class User < ActiveRecord::Base
    has_secure_password
    validates :username, presence: true, :uniqueness => { :case_sensitive => false }
    validates :password, presence: true, :length => {minimum: 6}
end
它可以工作,但它以“/users”而不是“/signup”呈现

这是正常的行为。
/signup
页面是来自
userscocontroller
new
操作的结果。此页包含表单。提交此表单后,数据通过
POST
方法从同一控制器转到
create
action

如果验证失败,控制器将呈现
:新建
模板,正如您所记得的
创建
操作有
/users
链接。因此,您将在
/users
链接中看到
:new
模板

这是
用户控制器的路线图

          GET    /users(.:format)                users#index
          POST   /users(.:format)                users#create
 new_user GET    /users/new(.:format)            users#new
edit_user GET    /users/:id/edit(.:format)       users#edit
     user GET    /users/:id(.:format)            users#show
          PUT    /users/:id(.:format)            users#update
          DELETE /users/:id(.:format)            users#destroy
为了满足您的要求,您可以将路由添加到
routes.rb
。并在表单中更改url

大概是这样的:

@user = User.create(user_params)
    if @user.save
        session[:user_id] = @user.id
        redirect_to stories_path, notice: "Thanks for registration!"
    else
        render 'new'
    end
post 'signup` => 'users#create`
形式如下:

<%= simple_form_for(@user, url: HERE_IS_SIGNUP_PATH) do |f| %>

<%= simple_form_for(@user, url: HERE_IS_SIGNUP_PATH) do |f| %>