I’ve been trying to use this book to derive my own project with rails 3.0.1 and so far it has worked well (after learning a few of the changes by googling them). I should mention that I’m using the 3rd Edition rather than the 4th because I prefer a paper copy to have, and buying both the e-book and paper copy from this website here would kill me on postage (I’m outside the US), regardless of the discount for getting the paper book and the ebook together.
I’ve been copying the code for the admin login word for word, so I don’t think it’s an issue with my project being different with other functionality, however here is my problem.
When I go to admin/login it gives me the usual screen, with the generated form, however no matter what I put into the name and password fields it reloads admin/login and gives me a routing error. The code should have it redirect to admin/index, but strangely it does not (and I don’t understand why one minute admin/login works and the next it doesn’t).
Here’s my code in login.html.erb:
<div class="myForm"> <% form_tag do %> <fieldset> <legend>Please Log In</legend> <div> <label for="name">Name:</label> <%= text_field_tag :name, params[:name] %> </div> <div> <label for="Password">Password:</label> <%= password_field_tag :password, params[:password] %> </div> <div> <%= submit_tag "Login" %> </div> </fieldset> <% end %> </div>
in the application controller:
def login if request.post? user = User.authenticate(params[:name], params[:password]) if user session[:user_id] = user.id redirect_to(:action => "index") else flash.now[:notice] = "Invalid username or password" end end end
And the User model (not private):
def self.authenticate(name, password) user = self.find_by_name(name) if user expected_password = encrypted_password(password, user.salt) if user.hashed_password != expected_password user = nil end end user end
On inspection of the Form tag on the html, Action=’admin/login’ so I’m not sure if that is what is causing it but I have restarted the server several times and nothing seems to be helping.
Does anyone happen to have any ideas?