Category Archives: Rails

My notes from Rails Camp #17

The 17th Rails Camp was held last weekend at the Hawkesbury north of Sydney. It was 3 nights of coding, drinking and socialising…without any phone or internet reception.

Reminders for myself for next Rails Camp with no internet or mobile phone reception

  • Get all the Dash documentation you can
  • Actually START your project before you go, so that you KNOW you have everything you will need there
  • Make a goal for what you want achieved by the end of the weekend, otherwise, the time flies, you get distracted and nothing gets achieved
  • Don’t tell your husband you will text him when you get there…

What did I actually get achieved at Rails Camp this time?

Continue reading

Leave a reply

Custom Mime Types for Beginners

Did you know that you can add Mime types (data types) to your Rails app to allow you to use the same URL for different response types?

For example, you might want to use your show method, e.g. /books/2,  with different responses depending on what MIME type you suggest.
e.g.
html response – showing information about your book in html
json response – information about the book in json
pdf response – a pdf download about book 2
circus response – a specially formatted version of the books/2 data

The last one, ‘circus’, is obviously made up, but you CAN create new Mime types, called whatever you like. So you might want to make the data-type called ‘circus’ available and have a special format return on that URL when the data type is Circus.

All you need to do is

1.  Add the Mime type if it isn’t already there
In config/initializers/mime_types.rb add
Mime::Type.register "text/circus", :circus
(Since it is in an initializer, you will need to restart your server to pick up this change)

2. Make the response available in your controller action
def show
respond_to do |format|
format.html # render html
format.circus { #render circus }
end
end

3. Add the view you want
In your views folder,  you would put the circus template into a show.circus.erb file

4. Then in your request, set your data-type as ‘circus’ (E.g. in your form or Ajax request).

 

Leave a reply

Rails Routes Files – For Dummies

Want to tidy up your routes files and make them generally more professional-coder-looking? Consider these ideas which I’ve come across in my first few weeks at my new job:

Concerns

You can use concerns to dry up your routes file. If many resources have the same list of things nested inside them you can list the nest once with a name like

concern :name_of_my_list do
  put the list in here
end

Continue reading

2 Replies

Gemfile vs Gemfile.lock

A bit of a clarification and reminder from an answer on Stack Overflow, as to what the difference is between Gemfile and Gemfile.lock:

The Gemfile is where you specify which gems you want to use, and lets you specify which versions.

The Gemfile.lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.lock and install the exact same versions, rather than just using the Gemfile and installing the most recent versions. (Running different versions on different machines could lead to broken tests, etc.) You shouldn’t ever have to directly edit the lock file.

Check out Bundler’s Purpose and Rationale, specifically the Checking Your Code into Version Control section.

 

Leave a reply