Archive for April, 2008

Dealing w/ main navigation in rails

Here?s a great plugin for doing navigation in rails

I didn?t like that too much.

A simpler yet elegant solution is here: simple & elegant

There is also this

And actually, what I usually do is just use the following code and then css it appropriately.

            <body id="<%= controller.controller_name %>">

Symbolic link

How to create a symbolic link: ln -s [TARGET DIRECTORY OR FILE] ./[SHORTCUT]

example:

ln -s /usr/local/apache/logs ./logs

or

ln -s #{current_path}/public /home/#{user}/public_html

or

ln -s apps/appname/public /home/username/public_html

or

ln -s ~/forums/vanilla /home/twinsta/public_html/forum

(this last one, make sure to put it in your Capfile in order to keep the forum working when you update your app)

MySQL command line cheat sheet

Here is a great cheat sheet for all those mysql command line commands in terminal

cheat sheet

Removing .svn files and folders

How to remove .svn files

find ./ -name ?.svn? | xargs rm -Rf

Completed ?Taxation ? Partnerships? UCR extension class with an A-


Deploying with Git and Capistrano

Get your git all setup and your project gitified.

Freeze your gems and freeze to edge your project and git push that to the server (rake rails:freeze:gems and rake rails:freeze:edge or rake rails:freeze:edge TAG=rel_2-0-2 see here ) (script/about to see what version your rails is already on. maybe you already froze earlier and forgot)

Capify your project (capify .)

Edit deploy.rb file created by capistrano use this instructions also use hostingrails? wiki for capistrano two Here?s an example of deploy.rb

Edit Capfile created by capistrano example code

Set ENV[?RAILS_ENV?] ||= ?production? in config/environment.rb

Edit .htaccess file to do fcgi instead of normal cgi instructions here

On local machine run cap -T to view commands available

Add an ssh-keygen to the server as well (since it will be taking from github) see here and here

Follow the latter end of these instructions from hostingrails

Freezing rails and gems

It?s a good idea to freeze your project before deploying. To do this run the command: rake rails:freeze:gems

And it?s a good idea to freeze your rails version to by doing: rake rails:freeze:edge

Here?s a tutorial

Accessing associated table data with rails.

To access data that has related data using has_many and belongs_to, first make sure you?ve set up your related ids. For example for the two models posts and attachments, the attachments table should have a field called post_id so that the attachment can be association to the post.

Then make sure you have the appropriate has_many and belongs_to in the models

Then do the following in your view code:

                <% @posts.attachments.each do |post| %>
    <img src="<%= attachment.public_filename %>" class="attachment" /><br />
<% end%>

Email validation/activation with rails

  1. Install the restful_authentication plugin
  2. run script/generate authentication user sessions ?include-activation
  3. rake db:migrate
  4. see this link
  5. on the part where you create the mail.rb config/initializer use this link instead, pasting that code in the place of the code recommended by avnetlabs.com?s tutorial
  6. create a new file called smtp_tls.rb in your lib folder and paste this code into it
  7. open the production and development configuration files, config/environments/production.rb and config/environments/development.rb and put in the SITE_URL = ?localhost:3000? and for production your webdomain. according to avnetlabs
  8. edit the user_mailer.rb model to use the SITE_URL variable instead
  9. Open the email template files (app/views/user_mailer/activation.html.erb and app/views/user_mailer/signup_notification.html.erb) and modify as desired
  10. that?s it

Resources

Rails find with conditional dates

To use the rails find method with conditional dates do the following. inspiration from here

Example:

            Record.find :all, :conditions => {:date => 20070400..20070431, :user_id => current_user.id}

Or addition: Record.sum :hours, :conditions => {:date => 20070400..20070431, :user_id => current_user.id}

Now what if you want to use the current month instead of the static typed in months above?

             this_month = Date.today.strftime("%Y%m")
start_of_month = this_month + '00'
end_of_month = this_month + '31'
result = Record.sum :hours, :conditions => {:date => start_of_month..end_of_month, :user_id => current_user.id} rescue nil
result = result.round(1) rescue nil

Rails Date method: