Can't get login to work

Hi, could anybody help me try to get a login function working, I have
created a database for users called user1, I have the login screen set
up but when i hit login with a correct username and password, nothing
happens

I am expecting the lgin button to direct the user to the main page and
show the user as logged on, but nothing happens! Thanks!

Controller

class User1Controller < ApplicationController
  def index
    @title = "Temporary View"
    @users = User1.find(:all)
  end

def register
    @title = "Register"

    if request.post?
      @user = User1.new(params[:user1])
      if @user.save
        session[:user_id] = @user.id
        flash[:notice] = "User with login #{@user.screen_name} created!"
        redirect_to :action => :index
      end
    end
  end

end

  def login
    @title = "Title"

    if request.post?
      @user = User1.new(params[:user1])
      user = User.find_by_screen_name_and_password(@user.screen_name,
@user.password)
      if user
        session[:user_id] = user.id
        flash[:notice] = "User #{user.screen_name} logged in"
        redirect_to :action => "index"
      else
        # Don't show the password in the view
        @user.password = nil
        flash[:notice] = "Invalid email/password combination"
      end

      end
    end

def logout
    session[:user_id] = nil
    flash[:notice] = "Logged out"
    redirect_to :controller => :site, :action => :index
  end

  end

The first thing to do is see whether the button click is sending you
to the correct controller/action.
Do you see what you expect in the log file when you click the button
(log/development.log)?
If not if you view the html of the page with the button (View, Page
Source or similar in your browser) does it look correct?
Have you validated the html of the page? Copy the complete html of
the page and paste it into the w3c html validator.

If still no joy post the view code and the bit of the log from when
you click the button.

Colin

Colin Law wrote in post #977179:

Hi, could anybody help me try to get a login function working, I have
created a database for users called user1, I have the login screen set
up but when i hit login with a correct username and password, nothing
happens

I am expecting the lgin button to direct the user to the main page and
show the user as logged on, but nothing happens! Thanks!

The first thing to do is see whether the button click is sending you
to the correct controller/action.
Do you see what you expect in the log file when you click the button
(log/development.log)?
If not if you view the html of the page with the button (View, Page
Source or similar in your browser) does it look correct?
Have you validated the html of the page? Copy the complete html of
the page and paste it into the w3c html validator.

If still no joy post the view code and the bit of the log from when
you click the button.

Colin

Hi , thanks for the reply

The development.log only shows info about database migrations

here is the login.html
<h1>Login</h1>
<%= error_messages_for :user %>
<% form_for :user do |f| %>
  <p>
    <%= f.label :screen_name %>:
    <%= f.text_field :screen_name %>
  </p>
  <p>
    <%= f.label :password %>:
    <%= f.password_field :password %>
  </p>
  <p>
    <%= f.submit "Login" %>
  </p>
<p>
  Not a member? <%= link_to "Register now!", :action => "register" %>
</p>

<% end %>

and view source in web browser is

<h1>Login</h1>

<form action="/Application/user1/login" method="post">
  <p>
    <label for="user_screen_name">Screen name</label>:
    <input id="user_screen_name" name="user[screen_name]" size="30"
type="text" />
  </p>
  <p>
    <label for="user_password">Password</label>:
    <input id="user_password" name="user[password]" size="30"
type="password" />
  </p>
  <p>
    <input id="user_submit" name="commit" type="submit" value="Login" />
  </p>
<p>
  Not a member? <a href="/Depot_Application/user1/register">Register
now!</a>
</p>

</form>

I get two errors ,one because i have not declared document type and one
that says the form element is not allowed...howeveer this is the code
that i have taken from a book, so i presume that it should work?

If you have a form element where it is not allowed then it is not
surprising that you have a problem.

Colin

Okay I see a problem

When I hover over the login button in the login page, the url for the
button is the same page...which is strange....because i have directed it
to go to the main page

hhhmm.I don't understand why login submit button staying on the same
page though?

I tried redirecting to a url via the login button but it still stays on
the same page , tried directing to another page in the rails
application, again nothing...any ideas?

We might have some hope of helping if you were to post the view code
with the login button on it. But fix the form tag first. On the
assumption that the button is the submit for the form then if the form
tag is invalid then the submit is unlikely to work.

Colin

Colin Law wrote in post #977241:

Okay I see a problem

When I hover over the login button in the login page, the url for the
button is the same page...which is strange....because i have directed it
to go to the main page

We might have some hope of helping if you were to post the view code
with the login button on it. But fix the form tag first. On the
assumption that the button is the submit for the form then if the form
tag is invalid then the submit is unlikely to work.

Colin

Hi again,Appreciate the response. the view codes should be above on the
third post in this thread , the first one is for the View and the next
one is the source code as it appears on the browser. Here is the code
relevant to the login button

Apps/View

<p>
    <%= f.submit "Login" %>
  </p>

Browser

<p>
    <input id="user_submit" name="commit" type="submit" value="Login" />
  </p>

and here is my controller login , which should direct the user to the
main page after they enter in their details...

def login
    @title = "Title"

    if request.post?
      @user = User1.new(params[:user1])
      user = User.find_by_screen_name_and_password(@user.screen_name,
@user.password)
      if user
        session[:user_id] = user.id
        flash[:notice] = "User #{user.screen_name} logged in"
        redirect_to :action => "index"
      else
        # Don't show the password in the view
        @user.password = nil
        flash[:notice] = "Invalid email/password combination"
      end

      end
    end

Colin Law wrote in post #977241:

Okay I see a problem

When I hover over the login button in the login page, the url for the
button is the same page...which is strange....because i have directed it
to go to the main page

We might have some hope of helping if you were to post the view code
with the login button on it. But fix the form tag first. On the
assumption that the button is the submit for the form then if the form
tag is invalid then the submit is unlikely to work.

Colin

Hi again,Appreciate the response. the view codes should be above on the
third post in this thread , the first one is for the View and the next
one is the source code as it appears on the browser. Here is the code
relevant to the login button

Sorry, I missed that in the previous post.
So have you managed to get the html through the validator yet? There
is no point investigating further until the html is valid.

Apps/View

<p>
<%= f.submit "Login" %>
</p>

Browser

<p>
<input id="user_submit" name="commit" type="submit" value="Login" />
</p>

and here is my controller login , which should direct the user to the
main page after they enter in their details...

There is no point posting this as, unless I misunderstand, it is not
even getting to this code.

Colin

Okay, changed code in line will a sample application which i have tested
to work and confirmed as working, and the same problem persists...

okay...

In my other view files asscociated with my other controllers the files
end in rhtml...ie index.rhtml

My files end in html for my user files...i.e index.html

Is this a problem?

I would expect it to be a problem. The current standard is to use
.html.erb which tells rails that it is to be interpreted as erb.
.rhtml is old but should still work ok I expect. I would be surprised
if .html worked for files with erb, but if it did not then <% ... %>
would not be interpreted so it would be fairly obvious. Are you
following an ancient tutorial? It is essential that the tutorial you
are following matches the version of Rails you are using (at least to
the major revision, Rails 2 or 3).

Colin