Search code examples
ruby-on-railsruby-on-rails-4rails-routing

rails automatically change routes for no reason


I have the following sign-up form in my signup_path, which is mapped to to users#new

In my 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%>

in my routes.rb

get "signup" => "users#new"

Right now I can successfully sign up new users at localhost:3000/signup, which is ideal. However, whenever I try to test the form by leaving some or all fields blank, it will redirect me to localhost:3000/users. I thought maybe something is off in my controller, but I can't find anything odd about it

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

What am i doing wrong here? Why is the route changing? I tried to redirect_to signup_path as oppose to render "new", but if I do that then the error messages would disappear, which I do not want.


Solution

  • Right now your login form posts to /users, so when you render "new" the url shown is correct. If you want to change that behavior, you could add post "signup" => "users#create" to your routes so you could post the form to /signup which would display your desired url when you render "new" from the create action.