Yum….

•April 24, 2008 • Leave a Comment

def password=(pw)
@password = pw
salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
self.password_salt, self.password_hash = salt, Digest::MD5.hexdigest(pw + salt)
end

Note that the line [Array.new(6){rand(250).chr}.join].pack("m").chomp is a good way to auto-generate passwords. ;) Have a bit of fun with it.

Rails :: Some minor view sorting

•April 22, 2008 • Leave a Comment

You would not believe the trouble I went to just to get the correct form of arranging ‘posts’ on the index of one of my side projects. Well, it was about 20 minutes’ trouble, but still more trouble than I would have liked!

The project is something I’m making for a friend to keep track of his DVD collection, which has the field :loaned as a boolean. What I was trying to do is make it so that there were two tables on the index page. One that listed available DVDs, and another to list those that were already loaned out and obviously their relevant information (which isn’t so important at this time).

So here’s how I managed it…. :/

def index
@loaned = Movie.find(:all, :conditions => { :loaned => true })
@not_loaned = Movie.find(:all, :conditions => { :loaned => false })
end

(Looks simple I know, but it took me at least 20 minutes to work out :| ). Then in the ‘index.html.erb’ file you will need something similar to…

....
Available to Loan
<%- for movie in @not_loaned -%>
<%=h movie.information_etc_etc %>
<%- end -%>
<br/><hr/><br/>
Loaned out
<%- for movie in @loaned -%>
<%=h movie.information_etc_etc %>
<%- end -%>
.....

And it works. Well, it should do. As a side note: the erubis tags <%- code -%> removes the double spacing in the Source View of your webpage, as opposed to the standard <% code %>. There is a RailsCasts episode that goes over this I believe. Just a tidbit for you. :p

Sorry for the realllllly bad code pasting. WordPress.com doesn’t like code pastes. :(

Restful Authentication for Rails 2.0

•April 8, 2008 • Leave a Comment

You should look to the following article link to find out how to do it for Rails 2.0. I won’t post here the whole thing as I’d only be copying (roughly) someone else’s work anyway. Just a few points to note though.

include AuthenticatedSystem
should go under your class declaration in the app/controllers/application.rb file. Also, in the application.html.erb layout you want (right have the very end of the document) the following:
<%= yield %>

Don’t be a twat and forget that like I did, and then sit there wondering why the hell it wasn’t showing the content.

Anyway, here’s the post: Restful Authentication Rails 2.0.

#01 – Deploying a Rails Application

•March 26, 2008 • Leave a Comment

This is a really, really simple way to create a new application in Rails 2.0. I will write more when I have the time so that this application is then further developed. However, for now you just need this to start work.The first thing you need to do is make sure Rails is properly install.I’m assuming you have Ruby installed already. If not, go to Ruby-Lang.org to find the relevant installation for your OS.

Open a command shell/prompt of your choice and run:

gem install rails --include-dependencies

..and go make a quick cup of coffee while it does its thing. Once install, you want to create a project folder for all your rails application. For simplicity’s sake I stuck this in MyDocuments\Rails (for my Windows development platform). This will store all your projects from now on. Use the command shell to navigate to the Rails folder you created and run:

rails MyProject

to create your project folders, etc. Rails follows the MVC paradigm. You can read more about this here.

Now you want to go into the project folder you just created. In the case of this tutorial it is ‘MyProject’. The next command you want to get things up and running quickly is the scaffold command.

ruby script\generate scaffold Todo title:string priority:integer done:boolean

This will create a ’skeleton’, otherwise known as a scaffold, for your project. The title:string priority:integer done:boolean part of the code is your database creation. This creates a column with the name of the word -before- the colon of a type specified -after- the colon.

You’re done. You now have a skeleton for you project. Go ahead and load up a server with the command:

ruby script\server

Open the URL http://localhost:3000/todos/ to see what you’ve just created.

NOTE: In the scaffold command we gave the scaffold a name of ‘Todo’. ActiveRecord will automatically pluralise this when you use the scaffolding method, so the URL turns into ‘/todos/’

Have a poke around the files within the project folder and see how the system works. Also, you should check out Akita on Rails for a more in-depth look at Rails 2.0

New page opening for business. Fantastic. x)

•March 19, 2008 • Leave a Comment

This is really just a quick test of some functions. Here goes.

def index
Users.find(:all)
end

Booo, seems that the wordpress.com version isn’t going to be good for my code pastyness