Ruby On Rails

    How to enable custom slugs options in latest version of refinerycms

    To enable custom slugs in refinerycms goto config/initializers/pages.rb you can see a variable use_custom_slugs uncomment that line and set that true if its false if its not there just add
    config.use_custom_slugs = true

    NoMethodError in nil class Ruby on Rails

    This error will come if you are missing method or don't have any record related to the path request so you need to handle this error simple add bang (!) in controller where you are getting data
    def request
        @request = Request.find_by_request_id!(params[:id])
        respond_to do |format|
           format.html {render :layout => "landing", :template => "requests/newrequest"}
           format.json { render :json => {:request => @request} }
        end
    end

    # ! rescue and  redirect to 404 page if no record exists for params[:id]

    ruby gem Mysql2 on windows 7 or ubuntu installation

    Install gem using following command on windows 7
    gem install mysql2 -- \\\\\'--with-mysql-lib=\\\\\"c:\\Program Files\\MySQL\\MySQL Server 5.0\\lib\\opt\\\\\" --with-mysql-include=\\\\\"c:\\Program Files\\MySQL\\MySQL Server 5.0\\include\\\\\"\\\\\'
    on ubuntu
    sudo apt-get install libmysql-ruby libmysqlclient-dev
    http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRail... this is to be placed in ruby/bin directory on windows libmysql.dll

    How to create page in refinery via code

      page = Page.create(
        :title => 'Testpage',
        :link_url => '/testpath',
        :deletable => false,
        :position => ((Page.maximum(:position, :conditions => {:parent_id => nil}) || -1)+1),
        :menu_match => '^/testpath(\/|\/.+?|)$'
      )
      Page.default_parts.each do |default_page_part|
        page.parts.create(:title => default_page_part, :body => nil)
      end

    How to get browser, operating system and client ip in rails

    You can do by adding these function in your controller or you can specify these as helper methods def get_browser if request.env['HTTP_USER_AGENT'].downcase.match(/crome/i) "Crome" elsif request.env['HTTP_USER_AGENT'].downcase.match(/msie/i) "Internet Explorer" elsif request.env['HTTP_USER_AGENT'].downcase.match(/konqueror/i) "Konqueror" elsif request.env['HTTP_USER_AGENT'].downcase.match(/firefox/i) "Mozilla" elsif request.env['HTTP_USER_AGENT'].downcase.match(/opera/i) "Opera" elsif request.e

    How to get month, year, day from date in rails

    To get month, year, day from datatime you can do that simply Suppose you have date 2012-02-20 08:56:00 UTC in post.published_at you have to extract day from it
    <?php
    = post.published_at.strftime("%d")
    ?>
    # Show 20 If you have to extract month Jan, Feb like this
    <?php
    = post.published_at.strftime("%b")
    ?>
    # Show Feb if you have to extract year
    <?php
    = post.published_at.strftime("%Y")
    ?>
    # Show 2012 Here is list of some date formats can be used %a - Abbrevated Day Name (Sun) %A - Full Day Name (Sunday) %b - Abrevatted Month Name (Jan) %B - Full Month Name (Janu

    incompatible character encodings: ASCII-8BIT and UTF-8 in Ruby 1.9.2

    incompatible character encodings problem is due to two three reasons first of all check if you are using mysql2 gem

    if not then gem 'mysql2' in your gem file run bundle install and change the adapter to mysql2 in database.yml file.

    it this doesn't work then you can specify default encoding in config/environments.rb file

    Encoding.default_external = Encoding::UTF_8
    Encoding.default_internal = Encoding::UTF_8

    before Application.initialize statement

    How to install refinerycms with mysql support

    To install refinerycms with mysql support for refinerycms >= 2.0.0
    mohit@ubuntu$ refinerycms <nameofapp> -d mysql

    How to route old blog urls to new 301 redirect for seo in routes.rb and using controller

    To do this: Put Following code in routes.rb
      ActionController::Routing::Routes.draw do |map|
        blogs = BlogPost.all.collect{|p| p.cached_slug}.join("|")
        unless blogs.blank?
          map.connect ':id', :controller => 'redirect', :action => 'index', :requirements => { :id => Regexp.new(blogs) }
        end
      end

    It redirects only when path matches with old blog post paths
    Create a controller redirect have following code class RedirectController < ApplicationController helper_method :check_blog, :check_pages def index if check_blog r

    How to fix openssl problem for ruby gems while running

    To fix this reinstall ruby with openssl
    $ rvm reinstall 1.9.2 --with-openssl-dir=/usr/local
    If the above does'nt fix then try
    $ rvm pkg install openssl
    $ rvm reinstall 1.9.2 --with-openssl-dir=$rvm_path/usr