background preloader


Facebook Twitter


MIGRATION. ACTIVE RECORDS. INSTALL. Ruby: Counting occurrences of an item in an array / enumerable. Ratyrate: Add Rating to Your Rails App. Basic AJAX in Ruby on Rails - Additional related article content can be found in the right sidebar. This article will teach you the basics of AJAX in Ruby on Rails. Updated on August 31st, 2015 for Rails 4.2 and Bootstrap 3.x! Quite often in the past, we had to result to a ton of different javascript to perform many different AJAX operations, luckily Ruby on Rails makes AJAX structured and easy. This article will teach you the basics of using AJAX in your Rails application. In this example, we will use twitter bootstrap to add some styling and functionality to our app. Gemfile: gem 'bootstrap-sass', '~>' Next, run a bundle install.

Terminal Commands: bundle install Next, create a file in your assets stylesheets folder called bootstrap_config.scss and add the following code. app/assets/stylesheets/bootstrap_config.scss: @import "bootstrap-sprockets";@import "bootstrap"; Next, add a require for the bootstrap javascript files to your application.js file. app/assets/javascripts/application.js: //= require bootstrap Phew!

Understanding Polymorphic Associations in Rails. Polymorphic associations can be tricky to understand and many times one can get confused about when to use them, how to use them and how they are different from other associations. In this article we will go over these issues. Introduction Active Record Associations are one of the most important features of Rails. Polymorphic association is part of these associations. Basics While writing some rails application you will run into situations when you have model associations that seem to be similar, for example lets assume you have Course and Lab models in your application.

Lets see how to implement it. Here ta_duty_id is a foreign key and ta_duty_type will tell model TeachingAssistant which model it is associated with. /db/migrate/********_create_teaching_assistants.rb Now our model is generated, to run the migration do rake db:migrate. By making Teaching Assistant belong_to ta_duty instead of any other model we have declared polymorphic association. /app/models/course.rb /app/models/lab.rb. Brush up Your Knowledge of Rails Associations. Rails 5 is coming very soon (it’s currently at RC1), so while preparing for this major version, it is high time to revisit the basics. Today we are going to discuss ActiveRecord associations. Associations make it much easier to perform various operations on the records in your code. There are multiple types of associations available: One-to-oneOne-to-manyMany-to-manyPolymorphic one-to-many In this article we are going to discuss all of them as well as some options for further customization.

The source code is available on GitHub. One-to-many Associations To start off, create a new Rails app: $ rails new OhMyRelations -T For this demo Rails 5 release candidate is used, but everything discussed in this article applies to Rails 3 and 4. A one-to-many association is probably the most common and widely used type. Let’s try it in practice. . $ rails g model User name:string $ rails g model Post user:references body:text t.references :user, foreign_key: true Of course, you can also say: $ rake db:migrate. Infinite Scrolling in Rails: The Basics. Pagination is a very common and widely-used navigation technique, and with good reason. First of all, consider performance.

Loading all the available records in a single query can be very costly. Moreover, a user may be interested only in a couple of the most recent records (i.e., the latest posts in a blog) and does not want to wait for all records to load and render. Also, pagination makes reading the page easier by not flooding it with content. Nowadays, many websites use a slightly different technique, called infinite scrolling (or endless page). In this article, I am going to explain how to implement infinite scrolling in place of classic pagination. First, we will prepare our demo project, implementing basic pagination using the will_paginate gem.

The provided solution will fallback to the default pagination if a user has javascript disabled in the browser. Other items that will be covered: The working demo can be found at Gemfile. Infinite Scrolling with Rails, In Practice. In the predecessor to this article, we set up a very simple blog with demo posts and implemented infinite scrolling instead of simple pagination. We used will_paginate and some javascript to achieve this task. The working demo can be found on Heroku. The source code can be found on GitHub. Today, let’s implement a “Load more” button instead of an infinite scrolling. This solution may come in handy when, for example, you have some links inside the footer and infinite scrolling causes it to “run away” until all the records are loaded. To demonstrate how this can be done, make the following changes to PostsController: posts_controller.rb def index get_and_show_posts end def index_with_button get_and_show_posts end private def get_and_show_posts @posts = Post.paginate(page: params[:page], per_page: 15).order('created_at DESC') respond_to do |format| format.html format.js endend And add a route: config/routes.rb get '/posts_with_button', to: 'posts index_with_button.html.erb application.css.scss [...]

Ruby on Rails: Download. Infinite Scrolling | Drifting Ruby. Infinite Scrolling with Rails, In Practice. GitHub - plataformatec/simple_form: Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup. Rails Application Layout. By Daniel Kehoe Last updated 3 August 2014 Rails application layout for HTML5. Shows how to set up an application layout with navigation links, messages for alerts and notices, and CSS styling for Rails. If You Are New to Rails If you’re new to Rails, see What is Ruby on Rails? , the book Learn Ruby on Rails, and recommendations for a Rails tutorial. What is the RailsApps Project? This is an article from the RailsApps project. Background The default application layout is where you put HTML that you want to include on every page of your website.

Every Rails application needs a well-designed application layout. Rails will use the layout defined in the file app/views/layouts/application.html.erb as a default for rendering any page. HTML5 Boilerplate The well-known HTML5 Boilerplate project has been recommending “best practice” tweaks to web pages since 2010. Front-end Frameworks Rails Layout Gem Rails Default Application Layout <! This is a barebones application layout. The Rails Layout Gem <! Viewport. Change data in migrations like a boss – Rails Guides. Changing data on change database schema in production is a common problem for Rails developers. Assume that you have a Rails project. Some day you decided to change the database schema and want to add some new column. Then you have to go through all your models and change actual data according this new schema. Currently there are solutions to overcome this.

But all of them have their disadvantages. You will see in this chapter them. This post tells about these disadvantages and how to get rid of the issue with the migration_data gem. Solutions with disadvantages There are many solutions to avoid the issue. Writing code in migrations without caution Duplicate classes in migrations Writing raw SQL in migrations Using seeds Other methods Now let’s look at all of them one by one and see what are problems have these solutions. Writing code in migrations without caution Say we are going to add a column to a User model and then update all users in our database. Duplicate classes in migrations.