views:

11

answers:

1

Hello,

I am currently doing the Ruby on Rails tutorial by Michael Hartl.

Currently, I am on this part of the PDF, http://railstutorial.org/chapters/sign-in-sign-out#sec:sessions

My problem is this...

When signing up a new user the code function correctly by creating a new user and automatically signing that user in. However I run into trouble after that.

Instead of displaying the users page or redirecting some place I am unable to redirect_to any page and instead I'm stuck at a blank localhost:3000/users screen.

This seems to be where the problem is, but it probably isn't.

  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        sign_in @user
        flash[:success] = "Welcome to the Sample App!"
        redirect_to users_path
      else
        render 'new'
      end
    end
  end

Please let me know if any more information could be helpful.

Thank you very much.

+1  A: 

You use a respond_to :format block and then you don't provide any formats. You only need a format block if you want your action to behave different for different formats of request: eg for regular html vs javascript/ajax. So, either ditch the format block like so

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to users_path
    else
      render 'new'
    end
  end

or put your code in an html block:

  def create
    @user = User.new(params[:user])

    respond_to do |format|
      format.html {
        if @user.save
          sign_in @user
          flash[:success] = "Welcome to the Sample App!"
          redirect_to users_path
        else
          render 'new'
        end
      }
    end
  end

The first is probably fine for you.

Max Williams
Thank you this works.
wonderpus
Vote me up and tick me then :)
Max Williams