Does anyone know a good authentication tutorial they can suggest? I've
tried several restful authentication ones and an authlogic one on
RailsCast. But with each one I try, something seems to be missing in
the tutorial and I can't get it to work. I'm new to Rails so it's
possible user error on my part.
I'm on Mac OS X 10.5 and I generate all my apps in the Sites
directory. Is there a better place to create these applications?
Does anyone know a good authentication tutorial they can suggest? I've
tried several restful authentication ones and an authlogic one on
RailsCast. But with each one I try, something seems to be missing in
the tutorial and I can't get it to work. I'm new to Rails so it's
possible user error on my part.
And the problems you're having are...?
I'm on Mac OS X 10.5 and I generate all my apps in the Sites
directory. Is there a better place to create these applications?
Yes -- almost anywhere else, unless you're using Passenger.
For a simple authentication you can try nifty_authentication from gem
nifty_generators.
Just install it, generate a nifty_layout and after that a
nifty_authentication.
It will create two controllers, one named users which will be for
creating users and the other named sessions which will control login
and logout.
Also be sure to create a map.root in the routes.rb and it will be
fully functional.
It hasn't got roles etc. though, if you need many roles then maybe it
is better to use something else.
But for most applications which you might need only an admin area it
is ok.
Authlogic has a tutorial project on github (authlogic_example). Download and run that, and then compare that to what you are doing. There is a good tutorial on the authlogic site, but it can still help to look at and test a working example.
The things that stich everything together are the require_user method in the application controller and the “before_filter :require_user” filters in the controllers you want to control access to. Also make sure your user table has the necessary fields.
If you do the tutorial, and double check against the example project, you should be fine.
I have Mamp installed so I tried setting the socket that location,
Socket: /Applications/MAMP/tmp/mysql/mysql.sock
The rake seemed to go through without problems. However, when I run
the server I get this message about a field missing from the database.
Message from browser-------------------------------------
ActiveRecord::StatementInvalid in Root#index
Showing app/views/root/index.html.erb where line #5 raised:
Mysql::Error: Table 'RATdev.invitations' doesn't exist: SHOW FIELDS
FROM `invitations`
Extracted source (around line #5):
2: <% unless logged_in? -%><% if_in_beta? do -%>
3: <%= error_messages_for :invitation %>
4: <p>We are currently in private beta. Please enter your email
address below and we will let know when an invitation becomes
available.</p>
5: <% uberform_for [:user, Invitation.new] do |f| -%>
6: <%= f.text_field :email, :label => "Your Email:" %>
7: <%= f.submit 'Submit' %>
8: <% end -%>
Below is the error I get when I create the database, I did a trace.
---------------------------------------------
iMac:restful_authentication_tutorial Meyer$ rake db:create --trace
(in /Users/Meyer/Rails/restful_authentication_tutorial)
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:create
Couldn't create database for {"socket"=>"/var/run/mysqld/mysqld.sock",
"reconnect"=>true, "encoding"=>"utf8", "username"=>"root",
"adapter"=>"mysql", "password"=>"root", "database"=>"RATdev",
"pool"=>5}, charset: utf8, collation: utf8_general_ci (if you set the
charset manually, make sure you have a matching collation)
---------------------------------------------
I kept the default socket the example had: socket: /var/run/mysqld/
mysqld.sock
Thanks for everyone's suggestions.
Kevin
Let's take a step back. You have both the Apple version of MySQL and
MAMP installed? Are you actually using this successfully or could
there be a database connection problem? To factor this out, I
recommend you change your adapter to "sqlite3" (and install the Ruby
sqlite3 gem). Then change database.yml as:
In my experience, it is possible to run more than one version of MySQL
on one machine, but you have to take pains to separate the sockets and
ports between the instances. In other words, you will be working with
something other than an "out of the box" installation. That could
account for your difficulty.
@ruchika I'm trying the one you suggested, but I have problem when I
get to the end where it says to create the database. I enter the
follow in the terminal: