Posts

Showing posts with the label Ruby on Rails

Rails 4 WITH NO Database

Sometimes , you may want to create a Rails app lication without ActiveRecord or a database. There might be situations like, Your application only store data using third party APIs, or storing in to files or might not have any persistent data at all. Since Rails stands for the common case (database backed app lication ), by default it fails to start the server without a database connection. You can simply build a Rails app without a database: Comment / Remove any database gems ( mysql, mysql2, sqlite, pg, mongoid, etc..) from your Gemfile and then run bundle.    Open your application.rb. By default, in Rails 4.0 you can see one line which requires all of Rails: require 'rails/all' This includes ActiveRecord also, and requires a database connection. Instead, you can include the specific parts of Rails that you gonna use. See below: require 'action_controller/railtie' require 'action_mailer/railtie' requ...

Rails4 , Faster loading with turbolinks

The Ruby on Rails version 4.0.0 has been just released. The team took one and half years for development, Rails 4 comes with some interesting new features such as  Turbolinks, improved caching and thread-saf e. Turbolinks c hanges your Rails application into a single page JavaScript application, that is, it doesn't load new pages but instead replaces the current page with new content from the server. [ This is similar to pjax , but here we dont need to worry about wh ich element on the page to replace, and combi ning the server-side response to add , Th e entire body will be replaced ] Few major points on Turbolinks 1. Less Execution time : - Turbolinks makes following links in your web application faster. Turbolinks  won't allow the browser to recompile the JavaScript and CSS between each page change/loading, it keeps the current page there itself and replaces only the body and the title in the head. Execution time of code depends. The more CSS a...

Rails Kaminari - Ajax pagination

Here is a simple way to implement AJAX pagination using  Kaminari gem. Suppose, We are having a Product Model with fields id and name. So, In the ProductsController , def index   @products =  Product.all.page(params[:page]).per(params[:per])    respond_to do |format|       format.js       format.html     end end in view file index.html.haml   #products     = render 'products'   #paginator     = paginate @products, :remote => true And create _products.html.haml (this will be rendered from index) with content   %table     @products.each do |product|      %tr        %td= product.name Finally, We have to create one more  file _index.js.haml with content $('#products').html("#{escape_javascript(render 'products')}"); $('#paginator').html("#{escape_javasc...

Rails mongoid has field model validation

This can be done only with a custom method validation, Such as           class Comment        include Mongoid::Document        include Mongoid::Timestamps         include ActiveModel::Validations              validate :must_be_friends              def must_be_friends           errors.add(:base, 'Must be friends to leave a comment') if hash[:firstname].nil?        end     end

Rails 3.2 Ajax Paperclip Image uploading

I was working on uploading images using paperclip gem . I wanted to use Ajax. I googled a lot, But every result says its not possible, that  you can't upload images using Ajax. Some Jquery plug-ins are available, But its not behaving like what I really needed in my project. Finally I got the solution for this problem. It is a common solution. First of all download the the js code and save it as jquey.form.js in assets/javascripts. Include in application.js like     //= require jquery.form Next is your view. Which may look like this     <div id="image_view">       <%=image_tag @model.image_ attr_name .url %>     </div> Next is your form to upload image [You can use bootstrap modal to render this form. Then it'll be more beautiful]     <%=form_for @model, :html => { :multipart => true, :id => "form_id" }, :url => url_for(:controller => 'controller...