0

So I set up my routes like so:

StartPoint::Application.routes.draw do
  get 'login' => 'sessions#new', :as => 'login'
  get 'logout' => 'sessions#destroy', :as => 'logout'
  get 'signup' => 'users#new', :as => 'signup'

  resource :home, controller: 'home'
  resources :users
  resources :sessions

  root 'home#show'
end

How ever when ever I go to sign up and fail to fill in proper data It always takes me to bla.com/users instead of signup. It should always go back to sign up. my controller looks like this, for new and create

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(user_signup_params)
    if @user.save
      redirect_to root_path
    else
      render :new
    end
  end
end

Some one told me that when it fails, order to see all the error messages I need to go back to to the new method, which then displays me all my errors. How ever the new method has a completely different url structure then the create. Regardless, if there are errors - I need it to go back to bla.com/signup, not bla.com/users

Adam
  • 609
  • 2
  • 8
  • 15
  • In Rails, because the controllers are based on a RESTful architecture, you need to have two different routes for `new` and `create`. You want to do both with the same route. Perhaps this question might be of use: http://stackoverflow.com/questions/2472393/rails-new-vs-create – Marcelo De Polli Sep 28 '13 at 23:21

2 Answers2

0

Shouldn't you have a post

post 'login' => 'sessions#create', :as => 'login'
get 'login' => 'sessions#new', :as => 'login'
get 'logout' => 'sessions#destroy', :as => 'logout'
get 'signup' => 'users#new', :as => 'signup'
Polygon Pusher
  • 2,865
  • 2
  • 27
  • 32
-1

The action of your form is /users. So, after POSTing to /users you either need to redirect to a different url (/signup), or just be satisfied with the /users endpoint. Note: if you redirect_to signup_path you will lose the data you POSTed.

Your best option might be to not use a resource for your signup.

match 'signup' => 'users#signup'

Then in your controller, handle both POSTs and GETs:

def signup
  if request.post?
    @user = User.new(user_signup_params)
    if @user.save
      redirect_to root_path
    end
  end
  render :new
end

Documentation for the request object.

whitehat101
  • 2,428
  • 23
  • 18
  • Um, you never defined the method post? its not like it, it doesn't understand what it is, in fact: `NoMethodError` – Adam Sep 28 '13 at 15:43
  • My mistake, I meant `request.post?` For some reason, I thought it was directly available in controllers. – whitehat101 Sep 28 '13 at 22:55