I’ve been having an issue. I use Elastic Beanstalk for deploying most of my applications – it’s a layer over the top of Amazon’s EC2 service that handles a load of the complexity for you. I simply define which platform I want (Ruby 2.3 with Puma), define any components that need to be installed on… Continue reading
Post Category → Views
How do I comment out code in my views?
You’ve been writing a view – it’s got quite a bit of code in there and you need to play around with a few bits and pieces. So it’s time to comment a chunk of the code out so you can adjust the layout. First try – let’s use some HTML comments <!– IGNORE ME… Continue reading
How do I reference a font or an image from my CSS files?
Rails uses a thing called the “asset pipeline” to pre-process your assets (Javascript, CSS, images and fonts). This has a number of advantages – Javascript get minified and squashed into a single file for faster transmission, and the same happens to your CSS as well. All your static assets (the single JS file, the single… Continue reading
How does Rails work?
So you’ve decided to learn Rails. You could dive straight in to learning the code, but Rails is an opinionated framework and it expects certain things to be done a certain way. A high-level understanding of how it’s structured and what goes where is always important. So let’s follow a hypothetical web-request on its journey… Continue reading
Multiple models in a single form
Following on from our form containing diverse controls, let’s look at how to update multiple models from a single form. Rails has a concept of “nested” models, generally represented by has_many relationships. As we’ve been dealing with people, let’s give our person multiple addresses:
1 2 3 4 |
class Person < ActiveRecord::Base has_many :addresses, dependent: :destroy accepts_nested_attributes_for :addresses, allow_destroy: true, reject_if: :all_blank end |
The first bit is simple enough, the second bit tells… Continue reading
How do I make a forms in Rails (part 2)?
After setting up a basic form, you probably need to fill it with some controls. Until the advent of HTML 5 (which I can’t rely on because many of my clients are still stuck with old browsers), there wasn’t really much choice with these controls. Rails offers helpers for generating these controls, and fills some… Continue reading
How does form_for work?
No real application is any use unless the user can enter information into it. And in a standard HTML application, or a web-site, the way to do that is through forms. The HTML form is a simple thing. It consists of the form itself, which when submitted, sends a request (normally a POST, but other… Continue reading
How do I prevent an AJAX link from being clicked twice?
I have a link that I want users to click when they “like” a POST. It’s implemented using AJAX and looks like this:
1 |
<%= link_to 'Like', post_like_path(@post), method: :post, remote: true %> |
First thing to note – I never use the Rails remote helpers. They are nice and easy to get started but you lose a lot of control. And if you use… Continue reading
How do I create an admin section for my app?
I’ve built the front-end of my app and it’s working well. Now I need to add in an admin section so that things can be configured. And I’d like it to look different to the front-end so the user knows they’re in the admin section. You want to separate your admin stuff from your other… Continue reading
Why shouldn’t I pass instance variables to views?
One of the design decisions in Rails that I thoroughly disagree with is the fact that instance variables are magically passed from the controller to the views. For example, take this example from yesterday:
1 2 3 4 5 |
class DocumentsController < ApplicationController def index @documents = current_user.documents end end |
This uses a load of Rails magic. It renders app/views/documents/index.html.erb for you and sends @documents to it; presumably in index.html.erb… Continue reading