background preloader

Asset Pipeline

Asset Pipeline
1 What is the Asset Pipeline? The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass and ERB. The asset pipeline is technically no longer a core feature of Rails 4, it has been extracted out of the framework into the sprockets-rails gem. The asset pipeline is enabled by default. You can disable the asset pipeline while creating a new application by passing the --skip-sprockets option. Rails 4 automatically adds the sass-rails, coffee-rails and uglifier gems to your Gemfile, which are used by Sprockets for asset compression: Using the --skip-sprockets option will prevent Rails 4 from adding sass-rails and uglifier to Gemfile, so if you later want to enable the asset pipeline you will have to add those gems to your Gemfile. 1.1 Main Features The second feature of the asset pipeline is asset minification or compression. More reading: Related:  assetrails

Updated for Rails 3: Using Paperclip and jpegcam to get pictures from your webcam into Rails View the complete source code for this post Here’s how to get pictures from your webcam into Rails using the Paperclip plugin and jpegcam. It is an update of the Rails 2.3.x version of this post. What we will do is create a Photo model and a Photos controller with actions new, upload and create to take care of the image creation. Start by creating a new Rails application: Create the Photo model: Create the Photos controller: Edit config/routes.rb to contain a photos resource: Download jpegcam and put webcam.js in your public/javascripts folder and webcam.swf and shutter.mp3 in your public folder. Edit the layout file app/views/layouts/application.html.erb and insert the following HTML inside the <head>-tag just below the other javascript_include_tag: Next, create app/views/photos/new.html.erb and make a div for the webcam contents: And the actual webcam javascript: That will run the actual webcam so you can see yourself :) But for taking a picture, we’ll need to add the following button: Then run:

Nested model form in Rails 4 - iRoller's notes Quick guide on how to build a simple survey application using Rails 4 and simple_form. There are some good topics on it on RailsCasts (Rails 3 and no simple_form): And also a bunch of questions answered on stackoverflow. Please use the official documentation to understand things you’re doing I just want to cover the most basic setup. I’m going to use haml instead of erb and simple_form for creating forms. Install all required gems using bundler We’re going to use has_many and belongs_to associations as the simplest for our case. Create additional columns First of all we need to create and additional column inside anwers and questions database tables containing an ID of associated object Run migrations Run migrations to get tables actually created Create associations between models Then we need to add the associations to our models. Edit app/models/answer.rb Edit app/models/question.rb Edit app/models/survey.rb Edit controllers Edit views

Asset Pipeline for Dummies - coderberry The Rails asset pipeline is very powerful, but often misunderstood. At the Utah Ruby User Group, most of the attendees aren’t sure how to use it fully in their Rails app. It’s considered as one of the magic features that Rails offers. I reference the word asset a lot in this article. In this article, I want to simplify the asset pipeline so it is better understood. Purpose The asset pipeline has three goals: precompile, concatenate and minify assets into one central path. Moving Parts The asset pipeline is powered by two technologies: Sprockets and Tilt, the latter being a dependency of the former (look at your Gemfile.lock if you don’t believe me). Sprockets performs the asset packaging which takes the assets from all the specified paths, compiles them together and places them in the target path (public/assets). Tilt is the template engine that Sprockets uses. Asset Paths Rails applications default to having three possible asset paths. The Manifest Here are some directives that can be used:

Pradeep's Ruby on Rails Blog | RoR for developers bootstrap-ruby/bootstrap-editable-rails Unholy Rails: Adding JavaScript to Rails by Daniel Kehoe Last updated 31 December 2012 Rails and JavaScript topics: How to include external JavaScript files and jQuery plugins in Rails 3.1 or newer. Using page-specific JavaScript in Rails. What is the best way to add a JavaScript library to a Rails application? If You Are New to Rails If you’re new to Rails, see What is Ruby on Rails? What is the RailsApps Project? This is an article from the RailsApps project. Contents Rules of Thumb In summary, here are rules of thumb to guide your use of JavaScript in Rails: Logically organize your site-wide scripts in the app/assets/javascripts/ folder. In almost all applications, there is no need to add external JavaScript libraries directly in a view template. The Rails asset pipeline will become even more important in Rails 4.0 with the new Turbolinks feature. Principles for Performance It’s difficult to sort out all the advice and folklore around Rails and JavaScript. The fewer <script> tags you use, the faster your pages will load. Summary

Getting Started with Rails 1 Guide Assumptions This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. Rails is a web application framework running on the Ruby programming language. If you have no prior experience with Ruby, you will find a very steep learning curve diving straight into Rails. Be aware that some resources, while still excellent, cover versions of Ruby as old as 1.6, and commonly 1.8, and will not include some syntax that you will see in day-to-day development with Rails. 2 What is Rails? Rails is a web application development framework written in the Ruby programming language. Rails is opinionated software. The Rails philosophy includes two major guiding principles: Don't Repeat Yourself: DRY is a principle of software development which states that "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." 3 Creating a New Rails Project 9 Security

Working with nested forms and a many-to-many association in Rails 4 - Created by Pete Recently a project I was working on needed a many-to-many relationship that would also store some extra data in the pivot table. Rails provides helpers to make working with this sort of relationship a breeze but when you start to include the nested forms and requirement to add data to that connecting table the solution may not be that obvious. I'll be using Rails 4, the code will be the same for Rails 3.2 for the most part the major difference is Strong Parameters is now used in place of attr_accessible. For this example I'm going to use a Survey application, unfortunately this was a survey done in the street on paper and now the results need to be manually added to the system. Each Survey will have some Questions, these Questions will be answered by a Participant. So in this example we need an Answers table to be our many-to-many table that will link our Participant to our Question and keep the Answer the participant provided in an additional column. So let's start a new Rails application.

A Simple Pattern to Namespace and Selectively Execute Certain Bits of JavaScript Depending on Which Rails Controller and Action are Active | Jerod Santo Rails 3.1’s asset pipeline compiles all of our JavaScript into a single file which is executed on all pages which include the javascript_include_tag method. Most of the time that method is in the layout, which means evey page gets the same bundle of JavaScript. This is great for caching, but not so great for selective code execution. I developed a pretty simple pattern to namespace and selectively execute certain bits of JavaScript depending on which Rails controller action is active. I talk about JavaScript, but my code samples are all in CoffeeScript. 1) Create an application object Let’s write a Rails app for accurately predicting weather patterns. Create app/assets/javascripts/ This is also a nice place to define common methods that will be used all across the application. All controller-level objects will be namescaped inside this object, so it must be specified first in the application manifest. 2) Add a JavaScript controller for each Rails controller

Setting up PostgreSQL for Ruby on Rails development on OS X | Will writes One of the reasons people used to give for using MySQL over PostgreSQL (just ‘Postgres’ from here on in) was that Postgres was considered hard to install. It’s a shame, because it’s a great database (I’ve been using it for personal and some work projects for years, like my current side project, sendcat). Luckily it’s now really simple to get it going on your Mac to give it a try. What this guide is This is a guide to getting PostgreSQL running locally on your Mac, then configuring Rails to use that for development. What this guide is not An advanced PostgreSQL guide.Suitable for using in production.Anything to do with why you might want to use PostgreSQL over any other database. Installation You can get binaries for most systems from the Postgresql site, but it’s even easier if you’ve got homebrew installed, if you haven’t got homebrew it’s worth it, pick it up here. With homebrew just run: $ brew install postgres You will get a load of output, but the most important part is this: $ rails s

12 Tips for the Rails Asset Pipeline | Sydney, Australia Ruby on Rails Developers Face it, you are a top developer, one of the best at your game, you eat lambda's for breakfast, refactor whole apps as an afternoon snack and bad code just trembles when you approach. You are at the top of your game and nothing stands in between you and the code you desire. Yet, the Rails asset pipeline still trips you up, not once, not twice, but just about every time you try and use it. Here's a list of things that I have learnt that aren't necessarily obvious, that can help you tame this wild and savage beast. 1. No CSS or JS files will be available to your app through the asset pipeline unless they are included in other files OR listed in the config.precompile directive (see #2 and #3 below). Only application.css and application.js are available by default of all the CSS and JS files. For example, you make a file in the assets folder called: app/assets/stylesheets/site.css.scss And you want to include this in your page. // = require_self // = require 'site' and then reference it with: 2.

#264 Guard Guard watches files and runs a command after a file is modified. This allows you to automatically run tests in the background, restart your development server, reload the browser, and more. Download: source codeProject Files in Zip (260 KB)mp4Full Size H.264 Video (17 MB)m4vSmaller H.264 Video (10.5 MB)webmFull Size VP8 Video (24.8 MB)ogvFull Size Theora Video (23.6 MB) Customizing Devise views in Rails #336 Copycopter Copycopter provides a nice interface that clients can use to edit the text in a Rails application. Learn how to deploy a Copycopter server using Heroku and integrate it in a Rails application through I18n. Download: source codeProject Files in Zip (84.4 KB)mp4Full Size H.264 Video (20.9 MB)m4vSmaller H.264 Video (9.79 MB)webmFull Size VP8 Video (9.75 MB)ogvFull Size Theora Video (20.8 MB)