Tag - Ruby on Rails

Ruby on Rails – Static PagesJuly
23

I have built an application recently but also wanted to integrated some VERY simple content management system (maybe an inline edit) for static pages on the marketing site. I saw a bunch of examples on how to do this but nothing really seemed to work for how I wanted to be able to tackle this problem. For starters, I wanted to be able to use other Rails Content and Models if I needed to display statistics or other Model info. I wanted to use the CSS and JS Packaging as well as layouts in Rails that I had already built. I also wanted to make the pages into subfolders for easier updating and organizing in the future, which plays into seo. Here is what I came up with to solve this issue. I hope it helps someone else as well…ENJOY :)

1) Rails Controller to handle the static page

This controller seems to be the general catch all for any file in any folder. The reason I set the path Param is so that we can use that to set the page name on the html body as a class

class PagesController < ApplicationController
  respond_to :html
 
  #Uncomment if you want cache caches_page :index, :show
 
  def index
    # Home Page Only = /
  end
 
  def show
    @path = params[:path] #Path Param (example /about/faq)
    template = File.join('public/pages', @path)    
    template_with_index = template + "/index"  
    render :template => template  #Normal File
    rescue 
         render :template => template_with_index  #if you had an index.html.erb file under a subfolder
    rescue 
         redirect_to "/404.html"
  end
 
end

2) Route

The route is a catch-all style route to be put after all other routes are listed

      resources :pages, :only => [:index, :show]
      match '*path' => 'pages#show'
      root :to => 'pages#index'

3) The Views and Layout

The updates to the views and layouts were to provide an easier way to write css

<body class="<%= page_name %>">

3) The Helpers

page_name is a helper method which will generate a variable based on the path params

module ApplicationHelper
 
  def page_name
    @path.parameterize('_') unless @path.blank?
  end
 
end

I felt this simple implement allows me to utilize all the beauty of Rails but doesn’t limit me to having to use public/*.html files, which isn’t easy to use when you already have layouts and styles. I thought about using some different gems to handle this but most seemed a bit overkill for what I wanted to do and this method was easier for me, in my opinion. I hope this helps someone else that is considering doing something like this as well.

Ruby on Rails – Easy date displayNovember
19

I have created this simple format_date method to format dates differently and put it in the ApplicationHelper. I know it isn’t something that is a super WOW factor but thought I would share anyways.

  def format_date(type, date)
    case type
      when :short_no_year 
        date.strftime("%b %d")
      when :short
        date.strftime("%b %d, %y")
      when :long 
        date.strftime("%B %d, %Y")
      when :long_day_of_week
        date.strftime("%a %B %d, %Y")
      when :default
        date.strftime("%m.%d.%Y")
    end
  end

to call in your view just use

<%=format_date(:long, crappy_date_from_db) %>

Ruby on Rails – Easy NavigationNovember
19

This is primarily going to be a repost to some extent but would like to add a piece that I think will help even more. Based on http://www.cuberick.com/2010/05/simple-tabbed-navigation-in-rails.html I have added a simple regex that I think will help with the current status and be flexible to change on the fly if rules change.

<ul>
<li class="<%= selected_navigation(:items) %>"><%= link_to "Items", items_path %></li>
<li class="<%= selected_navigation(:things) %>"><%= link_to "Things", things_path %></li>
</ul>

and then add in the ApplicationHelper this simple code

  def selected_navigation(element)
    request.fullpath =~ /\/#{element.to_s}/ ? "current" : ""
  end

Configure Ruby on Rails, MacPorts and Apache Passenger – Development EnvironmentJune
17

I started out by reading a bunch of articles on how to effectively set up a development environment on my Mac with Ruby on Rails, MacPorts and Apache Passenger. I started to find there were many different approaches on how to do this. I’m currently running MAMP for the development of websites for my clients, but couldn’t find the best approach for adding Ruby on Rails to my MAMP server. I tried a few different ideas but could never get the MAMP install to work with Ruby on Rails so I decided to set up MacPorts to a different ports (81) and run MAMP for PHP based web applications. I thought I would share my experience on how to set it up nicely on the mac running both ports 80 and 81 for different types of applications. Here’s how I did it!

1) Install MacPorts

Here are the instructions http://www.macports.org/install.php

2) Install Apache using MacPorts

$sudo port install apache2

You could decide to install MySQL, Subversion, Git, etc but for now those aren’t 100% important to getting up and running.

3) Update your user’s .profile

Update the export PATH with the apache2 line

export PATH=/opt/local/bin:/opt/local/sbin:/opt/local/apache2/bin:$PATH

Now we have a base Apache install with the path pointing to the Apache installed under MacPorts. You can run kick off the server and test it out at localhost.

$sudo apachectl -k start

4) Install the Ruby Components using MacPorts

$sudo port install ruby
$sudo port install rb-rubygems

5) Install Ruby Gems

$sudo gem install rails
$sudo gem install rake
$sudo gem install sqlite3-ruby
$sudo gem install mongrel

At this point, you should be able to go to the command line and type in and an application will be generated for you in a test app directory.

$rails testapp

If you cd into the testapp directory and type in

$script/server

and you should see this
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

At this point you are not using apache, you are using the server that comes with the Rails Framework. It proves everything is installed correctly at this point and you can hit Ctrl-C to shutdown the server. Next,

6) Install Passenger Phusion

$sudo gem install passenger

7) Install Apache Passenger

$sudo passenger-install-apache2-module

8) Install Passenger Preference Pane

Now we have everything installed but the biggest issue we have now is how do you add the Ruby on Rails applications to the Apache Passenger installation. Well, thankfully there is a Mac Preference Pane that can be added to help add your applications quickly to Apache.

Once you have installed the preference panel, you should be able to drag the testapp folder from above to the panel and it will automatically add the correct configuration to the apache server. Just for good measure, restart apache ($sudo apachectl -k restart) and you should be able to hit http://testapp.local/ and your ruby on rails application will show up.

Browse Archive