0

For those of you who actually have, or actually are watching RailsCasts, and for those of you who have actually watched these two videos: http://railscasts.com/episodes/250-authentication-from-scratch?view=comments http://railscasts.com/episodes/250-authentication-from-scratch-revised

you will notice that in both of them, on every login blank test he does, Ryan Bates doesn't seem to bother about the fact that upon login validation_presence error, he gets redirected from /sessions/new to /sessions. This has bothered me, and I am still in big trouble finding the solution to this? I have a feeling that it's fairly simple, but I guess it's only hidden someplace visible, but not to me.

*THIS IS BASICALLY RYAN BATES CODE:*He's got the error - I've got the error.: IDEAS???

USER MODEL:

has_secure_password

attr_accessible :email, :password, :password_confirmation

validates_uniqueness_of :email

SESSIONS CONTROLLER:

def new
end

def create
  user = User.find_by_email(params[:email])
  if user && user.authenticate(params[:password])
    session[:user_id] = user.id
    redirect_to root_url, notice: "Logged in!"
  else
    flash.now.alert = "Email or password is invalid"
    render "new"
  end
end

def destroy
  session[:user_id] = nil
  redirect_to root_url, notice: "Logged out!"
end

SESSIONS LOGIN PAGE:

<h1>Log In</h1>

<%= form_tag sessions_path do %>
  <div class="field">
    <%= label_tag :email %><br />
    <%= text_field_tag :email, params[:email] %>
  </div>
  <div class="field">
    <%= label_tag :password %><br />
    <%= password_field_tag :password %>
  </div>
  <div class="actions"><%= submit_tag "Log In" %></div>
<% end %>

1 Answers1

1

I don't have pro so can't watch the episode... why do you say you're being redirected to /sessions? Does the log file actually say it's doing that?

Remember... when you submit that form, it's going to POST to /sessions. So you'll see "/sessions" in the address bar, but you aren't being redirected there. And when that form submission fails, it's going to render :action => 'new' -- at the current url of "/sessions". But you still haven't redirected....

Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • Exactly, it's only POSTing to /sessions. That's what I came here to say! – Marcelo De Polli Jan 26 '13 at 23:26
  • Aren't both of them free? Okey, so it ain't redirecting. Well in that case how do I avoid that and make it keep the /sessions/new instead of having the link change to /sessions. I want to keep the look of it, and have later on in the project /sign_in keeping its link. How do major websites do it? Like Twitter for example? – HotChickFromAlaska Jan 26 '13 at 23:28
  • Hey mister Phillip. I don't know how to ditch this? How do I drop it, make it fly, remove it? Facebook, twitter and etc don't have it? – HotChickFromAlaska Jan 26 '13 at 23:48
  • If I remember the episode, Ryan is using the default routes for `resources :sessions`. That will give you the full shebang of HTTP verbs. Did you try overriding the POST one? – Marcelo De Polli Jan 26 '13 at 23:54
  • @HotChickFromAlaska If you want to keep it all at /sign_in, you will need to customize your routes... direct GET to the new action and POST to the create action. This deviates from how "resources :sessions" does it be default, but it's certainly an option. – Philip Hallstrom Jan 27 '13 at 00:57
  • Remember, I'm a newbie? You couldn't give ma little more noob friendly answer please? :) – HotChickFromAlaska Jan 27 '13 at 13:33