Blog

Sep
29

When to use Nested Controllers in your Rails apps

Posted by Michael Cindric in development, ruby on rails, Tutorials | 2 Comments »

Nested controllers are great. I don’t care that they have a stigma attached to them they work well and make sense especially if your rails application has a Admin section for example. This allows you to easy separate the admin logic into its own controllers etc keeping your code clean and easy to manage.

So thats what we are going to do for this example.

Step 1. Create your admin controller

You can use whichever process works best for you, but for this we are going to use script/generate:

 ./script/generate controller admin

Step 2. Create your user controller

You can use whichever process works best for you, but for this we are going to use script/generate:

 ./script/generate controller admin/users

Step 3. Check to ensure proper inheritance

The nested users controller should look something like so:

class Admin::UsersController < ApplicationController
end

Step 4. Create the Routes

The nested users controller should look something like so:

map.namespace :admin do |admin|
  admin.resources :users
end

Before rails 2+ you had to do like like we have below. Personally l don’t mind it makes it a little easier to read but either option works fine

map.resource(:admin) do |admin|
  admin.resources(:users, :controller => 'admin/users')
end

Step 5. Wait there is no step 5 your done

Now when you need to link to any of these actions its very simple keeping in mind that users is nested under the admin controller so all you have to do to create a link that goes to the index action on the nested users controller is add the admin prefix before

 link_to "Users", admin_users_url

The same applies when you are wanting to link to the show action of users all you do is loose the “s” on users as you would normally

 link_to "View User", admin_user_url(@user)

You can see a list of all the routes you have available to you by going to the root directory of your project in terminal and typing

rake routes
Sep
24

Scoping UserSession with Authlogic

Posted by James Kong in Uncategorized | 9 Comments »

I like using subdomains to create a more personalised web application experience for the user. Having their own URL to access the application allows them to feel a sense of ownership. There are many ways you can do this(click here for one) but I specifically want to look at scoping your user sessions so that users cannot login to other user’s accounts. Doing this with AuthLogic is surprisingly easy.

Assuming you subdomain names come from an Account model, add this line of code.

   class Account < ActiveRecord::Base
   authenticates_many :user_sessions
end

In your UserSessionController you can now scope your user sessions to the account.


class UserSessionsController < ApplicationController

    def create
       @user_session = @current_account.user_sessions.build(params[:user_session])
       if @user_session.save
          flash[:notice] = "Login successful!"
          redirect_back_or_default dashboard_path
        else
          render :action => :new
        end
    end
end
Sep
18

Using passenger to run your rails applications when developing

Posted by Michael Cindric in development, mac osx, passenger, ruby on rails | No Comments »

If your like me and sick of having to run ./script/server every time you want to start your rails app using passenger locally is for you then. First thing is to install passenger like so

sudo gem install passenger

Then you need to install the apache module for passenger.

sudo passenger-install-apache2-module

Now once you do this you will be told to all the following lines to your httpd.conf file in apache. Now you can just add these lines no problems but l like to have it in its own conf file and since apache will also load all conf files in /etc/apache2/other/ directory just create a passenger.conf file and add those lines in. Now these lines are different depending on how you installed ruby. I installed it in user/local keeping the orginal ruby version that ships with mac clean so mine looks like so

LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/local/bin/ruby

To create the new passenger.conf file just run the below command assuming you have textmate installed.

mate /etc/apache2/other/passenger.conf

Once you have cut and pasted those lines in save it and close the file. You then need to download the passenger preference pane. Here is a link to the latest build passenger-preference-pane1.3. Download and install then fire it up.

passenger preference pane Using passenger to run your rails applications when developing

Now unlock it if need be and click on the + button to add a new site. A window will pop up and now just select the root of your rails app and click “open”. You can now change the address if need be but just defaults to (rails_app_name).local. Hit apply and your good to go.

So go back to your browser and type in that address and it will fire up and your up and running. Now you wont see the log like you are used to since your not running webrick etc but all you have to do is tail the development log by going to the root dir of your application in terminal and typing the following

 tail -f log/development.log

And that’s it. You will need to restart passenger if you change the environment.rb file but thats simple enough via the preference pane. So no more ./script/server yay

Sep
15

no such file to load — capistrano/ext/multistage

Posted by Michael Cindric in development, mac osx, ruby on rails | 7 Comments »

I ran into this error the other day when running “cap -T”

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’: no such file to load — capistrano/ext/multistage (LoadError)

So to fix this l did the following

sudo gem uninstall capistrano
sudo rm -rf /usr/bin/cap
sudo gem install capistrano-ext
sudo gem install capistrano

Needed to do “sudo rm -rf /usr/bin/cap” as doing just the uninstall of capistrano didn’t remove all the files l needed it to.

Sep
11

Setting up a new Rails app with Git

Posted by Michael Cindric in development, ruby on rails | No Comments »

Ok so you want to be cool like all other rails developers out there and use Git for your projects. Here is a quick run down of how you might go about it.

Firstly create your rails app and using terminal go into the root of that project.

Then create a top, project-level .gitignore file. I use textmate so this can be done like so

$ mate .gitignore

Then add the following to the .gitignore file

.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3

You can also add more directories or files in here to ignore like css files if using sass for example, or your uploads directory.

Create some .gitignore files so the empty directories get tracked:

$ touch log/.gitignore
$ touch tmp/.gitignore

and finally commit that bad boy

$ git add .
$ git commit -m "First commit"

Running git add will tell git to track all the new files (Since first commit thats all of them). The commit will commit to your local Git repository and all that is left to do would be to add it to GitHub for example and your in business.

Sep
09

Restarting your rails application after deployment on Passenger

Posted by Michael Cindric in development, ruby on rails | No Comments »

This is something that l always forget to add the first time l deploy my rails applications that run on Apache + Passenger.

namespace :deploy do
  desc "Restarting mod_rails with restart.txt"
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "touch #{current_path}/tmp/restart.txt"
  end

  [:start, :stop].each do |t|
    desc "#{t} task is a no-op with mod_rails"
    task t, :roles => :app do ; end
  end
end

This will tell Capistrano to restart the server the passenger way by touching the restart.txt file in the tmp dir rather then trying to restart using mongrel. Lets hope l don’t forget next time