# Simple rails login system

**URL:** <https://www.ruby-forum.com/t/simple-rails-login-system/140551>\
**Category:** Rails\
**Created:** [June 16, 2008, 5:17am UTC](https://www.ruby-forum.com/t/simple-rails-login-system/140551 "2008-06-16T05:17:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [June 16, 2008, 5:17am UTC](https://www.ruby-forum.com/t/simple-rails-login-system/140551/1 "2008-06-16T05:17:23Z")

</div>

Hey All,

```
Just started to play around with rails a bit today by creating a

```

login system, but I’ve run into a glitch.

###user\_controller.rb###

class UserController \< ApplicationController

def login  
@title = “Log In”  
if request.post? and params[:user]  
@user = User.new(params[:user])  
@userfind =  
User.find\_by\_username\_and\_password(@user.username,@user.password)

```
            if @userfind

                    redirect_to :action => "index"

            else
                    render :text => "Login Failed"
            end
    end

```

end

def index

```
      db = Mysql.connect('localhost','root','godsp33d','netv_dev')
    feed_url = db.query("SELECT feeds FROM users WHERE username =

```

‘#{@user.username}’ AND password = ‘#{@user.password}’")

```
    feed_url.each do |x|
            x.each do |b|
                    @feeds = b.split(",")
            end
    end

```

@feeds.each do |links|  
open(links) do |http|  
response = http.read  
result = RSS::Parser.parse(response, false)  
result.items.each\_with\_index do |item, i|  
output += “#{i + 1}. \<a  
href=”#{item.link}"\>#{item.title}   
"  
end

# output += “============================================”

```
    end

```

end

end

def register

```
      @title = "Register"
      if request.post?
              @user = User.new(params[:user])
              if @user.save
                      render :text => "Account Created."
              end
      end

```

end  
end

## user\_model.rb

class User \< ActiveRecord::Base  
end

* * *

Basically the problem occurs in ‘index’, @user keeps returning nil only  
in index (works fine in register) when pushed through the sql query it  
returns nil. Is there a proper method to extracting the entered  
information via html form to push it through an SQL query?

My apologies for the formatting, Copied the text while working in  
fullscreen.

Thanks for any suggestions you can throw my way,

- Mac

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [June 16, 2008, 5:32am UTC](https://www.ruby-forum.com/t/simple-rails-login-system/140551/2 "2008-06-16T05:32:22Z")

</div>

Hmm…not sure what the problem with the code is…if you’re interested  
in a fast login system, you can try the acts\_as\_authenticated plugin.

Just type sudo ruby script/plugin install  
[http://svn.techno-weenie.net/projects/plugins/acts\_as\_authenticated/](http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated/)

Then in your application.rb file, you can just type:  
include AuthenticatedSystem and before\_filter :login\_from\_cookie

With it installed, you get some scripts to generate things. So:  
ruby script/generate authenticated user account

gives you some migrations and some views to play around with.

On the other hand, if you want to build it homegrown, I totally  
understand.

Ron

You can then put before\_filter :login\_required before any controller  
you want password protected.  
On Jun 15, 8:17 pm, Michael L. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [June 16, 2008, 6:32am UTC](https://www.ruby-forum.com/t/simple-rails-login-system/140551/3 "2008-06-16T06:32:04Z")

</div>

Ron wrote:

> On the other hand, if you want to build it homegrown, I totally  
> understand.

Yeah, I saw and checked out a few things revolving around those  
generators, but I’m more out for knowledge than a quick solution that I  
would have no general idea on how it works.

Thanks a lot for the info though!

- Mac
