small medium large xlarge

Generic-user-small
22 Nov 2010, 14:10
Chris (4 posts)

Hello,

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?

Thanks

Generic-user-small
22 Nov 2010, 14:47
Chris (4 posts)

Got it, by commenting out:

match ':controller(/:action(/:id(.:format)))'

in routes.rb.

Possibly not the best thing to do, but since it works it atleast allows me to move forward.

Jf_01_pragsmall
20 Dec 2010, 16:25
Juan F. Capristán W. (1 post)

Hi Chris, thanks for pointing out file routes.rb. This message was driving me mad since yesterday though I was not working on the book’s example but on my own app, guided by the book. I think it’s a big mistake not mentioning the need of checking the contents of this file whenever we create model independent controllers. In my case I am working with Rails 3.0.3 and I don’t see that line on routes.rb, I only see “get” declarations for my model independent controllers but no “post” declaration. In my case the problem was solved when I added the line:

post “admin/login”

(i.e. post “controller/action) on this file.

You must be logged in to comment