Tag - Rails 3

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
Browse Archive