Posts

Showing posts with the label ruby. rails

Write simple html text in into rails tags

You can write simple text inside rails tag like link_to button_tag, submit_tag, text_field_tag etc.. (raw("<strong class='text-danger'>Decline</strong")) example : <%= link_to  (raw("<strong class='text-danger'>Decline</strong")), welcome_path %>

install postgresql 9.5 postgis 2.1 on ubuntu 16.04 || postgis install in ubantu

First you have to install postgres if you have already install then run this command psql --version it return in my system psql (PostgreSQL) 9.6.2  Now we have to install postgis sudo apt install postgis postgresql- your-pg-version -postgis-2.3 in my case sudo apt install postgis postgresql-9.6-postgis-2.3 create extension for your database sudo -u postgres psql -c "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;" your_db_name  In my case sudo -u postgres psql -c "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;" gisdata Note: Remember before running this command please make sure you have not created extensions in your migration if you created extension in migration file then you must have to skip this command if you run this command by mistake then comment your extension create migration and run your migration file else you will get an error " Extension already created "

How to customise active admin page on keypress

Image
How to customise active admin page on keypress First make new route for getting filtered data: get '/admin/gyms/search_user', :to => 'admin/gyms#search_user', as: :search_gym_user In your controller create new method def search_user      @user =  User.where("name = ? AND role = ?", params[:name], "none") if params[:name].present?      respond_to do |format|        format.json  { render :json => @user }      end    end Render partial and add a id to to table panel "Gym Users:", :id => "foo-panel" do      # renders app/views/admin/posts/_search_users.html.erb       render 'search_users', { users: gym.users }      table_for gym.users, id: "search_gym_user" do        # binding.pry        # filter :"name" , :as => :select, :collection => gym.users.pluck(:name)        column "Joined" do |user|          distance_of_time_in_words(user.created_at, Time.now)    

If we are getting problem with FFMPEG || ffmpeg thumbnailer error in rails

If we are getting problem with FFMPEG  || ffmpeg thumbnailer error in rails Safely Remove FFMPEG First sudo apt-get --purge remove ffmpeg sudo apt-get --purge autoremove sudo apt-get install ppa-purge sudo ppa-purge ppa:jon-severinsson/ffmpeg sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install ffmpeg And try if you still have problem then, apt-get update apt-get install build-essential git curl gawk useradd -m guest su guest cd export LOCAL_BREW_ENV="$HOME/my_graphviz" mkdir -p "$LOCAL_BREW_ENV" git clone https://github.com/Linuxbrew/brew.git "$LOCAL_BREW_ENV" export PATH="$LOCAL_BREW_ENV/bin:$LOCAL_BREW_ENV/sbin:$PATH" export MANPATH="$LOCAL_BREW_ENV/share/man:$MANPATH" export INFOPATH="$LOCAL_BREW_ENV/share/info:$INFOPATH" export HOMEBREW_CACHE="$LOCAL_BREW_ENV/var/cache" export HOMEBREW_LOGS="$LOCAL_BREW_ENV/va

how to run rake task from console

how to run rake task from console In your file_name.rake namespace rake_task   namespace first_rake     task task_first do       // some code     end     task task_second do        // some code     end   end end Now run this task rake rake_task:first_rake:task_first

An error occurred while installing rmagick (2.16.0), and Bundler cannot continue

An error occurred while installing rmagick (2.16.0), and Bundler cannot continue You have to install library of rmagick in your system. sudo apt-get install imagemagick libmagickcore-dev libmagickwand-dev

Show Escape or Enter and tab in column or row in Rails

Show Escape or Enter and tab in column or row in Rails @users.each do |user|      raw(user.address.gsub(/\n/, '<br>'))   end //assuming you have \n or enter in your record and if you have another Special character then replace that character wiht respective html tag

Custom order of Record | | Order as specified || order_as_specified

Custom order of Record | | Order as specified || order_as_specified Install gem gem 'order_as_specified' bundle install or gem install order_as_specified write this line at top of your model / /that model you have to fetch record in custom order extend OrderAsSpecified Now in your controller method ids=[1,3,5,6,7,4,2] // assuming you have 7 record with this id's & and you want records in this order then Model.where(id: ids).order_as_specified(id: ids) //this will give you record in custom order Here is Working Example def index    premia1 = []    premia2 = []    PremiumInstruction.all.select{|pi| premia1 << pi.premium}    Premium.all.select{|p| premia2 << p if p.premium_instructions.blank?}    permia = premia2 + premia1.uniq //add both array    ids = permia.pluck(:id) //getting ids from array into an array //learn about pluck in other post    @collection = Premium.where(id: ids). order_as_specified (id: ids) end Here we

Send email in rails

Sending Emails Using ActionMailer and Gmail Create new rails application rails new new_app_name rails g scaffold user name:string email:string contact:string rake db:migrate rails g mailer example_mailer In  app/mailers/user_mailer.rb class UserMailer < ActionMailer::Base    def sample_email(user)     @user = user     mail(to: @user.email, subject: 'Sample Email, text:"simple text"')   end end app/views/user_mailer/sample_email.html.erb <!DOCTYPE html> <html>   <head>     <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />   </head>   <body>     <h1>Hi <%= @user.name %></h1>     <p>       Sample mail sent using smtp.     </p>   </body> </html> /config/application.yml gmail_username: 'username@admin.com' gmail_password: 'Gmail password' /config/environments/production.rb config.action_mailer.deliver

Generate Controller, views into specific folder

Generate Controller, views into specific folder rails generate scaffold_controller api/v1/users

convert string value into fractional in ruby on rails

Convert string value into fractional self.try(:shipping).fractional.to_f   // this will convert string value to fractional example- if  self.try(:shipping)  =>   #<1500 Euro shiiping> then   self.try(:shipping).fractional.to_f will return 1500

Rational Number or Rational Mothod in ruby or rational operation

Rational Number or Rational Mothod in ruby or Rational operation Rational Number is always comes with paired integer Number(1 ,2 ,3 .... && -1, -2, .....) x/y (where y>0). Rational (1) => (1/1) Rational (2, 5) =>(2/5) Rational(4, -8) => (-1/2) 5.to_r =>(5/1) 2/3r => (2/3) Rational into Numeric rational*numeric -> numeric (perfoms multiplecation) Rational(1, 3)  * Rational(2, 3)   => (2/9) Rational(200)   * Rational(1)      => (200/1) Rational(-5, 4) * Rational(-4, 5)  => (1/1) Rational(9, 4)  * 4                => (9/1) Rational(20, 9) * 9.8              => 21.77777777777778 rat ** numeric => numeric (Performs exponentiation) Rational(3)    ** Rational(3)    => (27/1) Rational(10)   ** -2             => (1/100) Rational(10)   ** -2.0           => 0.01 Rational(-4)   ** Rational(1,2)  => (1.2246063538223773e-16+2.0i) Rational(1, 2) ** 0              => (1/1) Rational(2,