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

On 24 January 2011 16:25, Simon M. [email protected] wrote:

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

Colin L. wrote in post #977179:

On 24 January 2011 16:25, Simon M. [email protected] wrote:

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

Login

<%= error_messages_for :user %> <% form_for :user do |f| %>

<%= f.label :screen_name %>: <%= f.text_field :screen_name %>

<%= f.label :password %>: <%= f.password_field :password %>

<%= f.submit "Login" %>

Not a member? <%= link_to "Register now!", :action => "register" %>

<% end %>

and view source in web browser is

Login

Screen name:

Password:

Not a member? Register now!

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?

On 24 January 2011 17:20, Simon M. [email protected] wrote:

The first thing to do is see whether the button click is sending you

<% form_for :user do |f| %>

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

On 24 January 2011 17:27, Simon M. [email protected] wrote:

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

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?

On 24 January 2011 21:12, Simon M. [email protected] wrote:

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.

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 with a sample application which has a login
screen which i have tested and confirmed as working, and the same
problem persists…

Visually the problem it evident that the login button has the same url
path as the login page, hence why when i click on the button nothing
happens and it stays on the same…I’m not sure how to change that…

I think the code is all fine. Does that mean another controller is
screwing up my login page?

Colin L. wrote in post #977241:

On 24 January 2011 17:27, Simon M. [email protected] wrote:

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

<%= f.submit "Login" %>

Browser

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

On 24 January 2011 22:02, Simon M. [email protected] wrote:

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