【问题标题】:rails automatically change routes for no reasonrails 会无缘无故地自动改变路线
【发布时间】: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


    【解决方案1】:

    现在您的登录表单发布到/users,所以当您render "new" 显示的网址是正确的。如果你想改变这种行为,你可以将post "signup" =&gt; "users#create"添加到你的路由中,这样你就可以将表单发布到/signup,当你通过create操作render "new"时,它会显示你想要的url。

    【讨论】:

    • 我在我的路线中添加了 post "signup" => "users#create",但不幸的是行为仍然相同。我是否遗漏了什么,尤其是“将表单发布到 /signup...”这一行?
    • &lt;%= form_for(@user), url: '/signup' do |f| %&gt; 应该发布到正确的位置。
    • 它给了我一个语法错误。我已经尝试了上述方法和:url => signup_path,但都返回语法错误。 Rails 4.1 有什么新东西吗?
    • 我修好了!原来 url 需要在 form_for 的参数内,例如
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2017-08-08
    • 2019-11-02
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多