Ruby On Rails

    How can we access rails helper function inside controllers globally

    Create and function in ApplicationHelper and include that helper in ApplicationConroller sepcifying the name of helper metod
    #application_controller.rb

    class ApplicationController < ActionController::Base
          include ApplicationHelper
            helper_method :acessed_anywhere

    end

    #application_helper.rb

    module ApplicationHelper

      def acessed_anywhere

      end
    end

    How to save multiple select in rails?

    To save multiple select value in rails use text field on database and save comma seperated or serialized values there or if you have to use the values in search results you can create modal associations for the corresponding fields

    How to convert date to timestamp and timestamp to date in ruby on rails?

    You can do this simply
    #date to timestamp

    start_date = DateTime.now

    timestamp = start_date.to_time_to_i

    #Timestamp to date again

    start_date_again = Time.at(timestamp)

    `require': cannot load such file -- client_side_validations/formtastic (LoadError)

    The problem seems to be due to client_side_validations version

    so use

    gem 'client_side_validations', '3.1.0'

    in gemfile and run bundle update

    How to add html to link_to in ruby on rails

    To add html inside link_to use raw on title
    <%=link_to (raw("<span>Link Title</span>"), path) %>
    Removing Jetbrains IDE idea files git rm -r --cached .idea/

    expected Array (got Rack::Utils::KeySpaceConstrainedParams) Problem

    This is due to missing key in [] when you are creating form for child attributes. Multiple entries are there and this behaves as array instead of hash causing problem. Solution is to add key there you can use timestamp as ke as that is unique

    How to get key from value ruby on rails hash or array

    To get key from value you can use below code:
    testhash = {:active => 1, :blocked => 2, :pending => 3}

    testhash.map{ |k,v| v==2 ? k.to_s.capitalize!  : nil }.compact

    It will display result "Blocked"

    active_support/dependencies.rb:503:in `load_missing_constant': Load error in module

    This is due to missing module file like my_module.rb in my_module directory for Module named MyModule

    or

    Due to duplicate class existence

    How to use observers in Ruby on Rails

    We can use observers with model to seperate some of the required functionality on paticular action To create a observer create observer file in models directory for example i have to create a observer for User model i can name it user_observer.rb it will send email on registration i have already created a mailer CustomMailer just call it here passing user object in it
    # models/user_observer.rb
    class UserObserver < ActiveRecord::Observer

      def after_create(user)
        # Send Registration Email
        CustomMailer.registration_email(user).deliver
      end
    end
    To ena

    How to uninstall RVM Ruby Version Manager

    To uninstall RVM just execute following command in terminal use sudo rvm implode if don't have administrative previlages for the logged in user remove /etc/rvmrc and ~/.rvmrc after uninstalltion
    rvm implode
    # This will uninstall the RVM