Changing the default date and time format in Rails 4

Found a nice approach through the Rails Internationalization (I18n) API Data and time formats can be ‘translated’ by adding the format to the i18n configuration. config/locales/en.yml en: date: formats: default: “%d/%m/%Y” time: formats: default: “%d/%m/%Y %H:%M” Note: remember to not have tabs for the indent, like I did first time 🙂 As mentioned by NoelProf … Read more

HTML Form: POST an array of objects

tl;dr: Add empty brackets ([]) after students to the input names. Fiddling with Rack::Utils.parse_nested_query it seems you can get the payload you want like this: <!– first student –> <input type=”text” name=”students[][first]”> <input type=”text” name=”students[][last]”> <input type=”text” name=”students[][age]”> <!– second student –> <input type=”text” name=”students[][first]”> <input type=”text” name=”students[][last]”> <input type=”text” name=”students[][age]”> Note the empty brackets … Read more

Refresh token using Omniauth-oauth2 in Rails application

Omniauth doesn’t offer this functionality out of the box so i used the previous answer and another SO answer to write the code in my model User.rb def refresh_token_if_expired if token_expired? response = RestClient.post “#{ENV[‘DOMAIN’]}oauth2/token”, :grant_type => ‘refresh_token’, :refresh_token => self.refresh_token, :client_id => ENV[‘APP_ID’], :client_secret => ENV[‘APP_SECRET’] refreshhash = JSON.parse(response.body) token_will_change! expiresat_will_change! self.token = refreshhash[‘access_token’] … Read more

strong parameters permit all attributes for nested attributes

The only situation I have encountered where permitting arbitrary keys in a nested params hash seems reasonable to me is when writing to a serialized column. I’ve managed to handle it like this: class Post serialize :options, JSON end class PostsController < ApplicationController … def post_params all_options = params.require(:post)[:options].try(:permit!) params.require(:post).permit(:title).merge(:options => all_options) end end try … Read more

Ruby on rails 4 app does not work in iframe

This has to do with Rails 4 enabling additional security protocols by default: http://weblog.rubyonrails.org/2013/2/25/Rails-4-0-beta1/ The setting that breaks iFrames on remote sites is X-Frame-Options. By default, this is set to SAMEORIGIN, which prevents the content from being loading cross domain: config.action_dispatch.default_headers = { ‘X-Frame-Options’ => ‘SAMEORIGIN’ } You can read about the new default headers … Read more

How to rename rails 4 app?

Since rails 4.1.x, if you want to rename your application, the only two files you need to modify are config/application.rb: require File.expand_path(‘../boot’, __FILE__) require ‘rails/all’ # Require the gems listed in Gemfile, including any gems # you’ve limited to :test, :development, or :production. Bundler.require(*Rails.groups) module YourApplicationName # <– rename it here class Application < Rails::Application … Read more

Rails 4: Why are fonts not loading in production?

We had this problem last week – the problem is that your assets will be compiled to have MD5 hashes on them, whilst your standard CSS will still be looking for their “standard” names. This is a problem with images & fonts. @font-face { font-family: ‘akagi’; src: asset_url(‘fonts/akagi-th-webfont.eot’); src: asset_url(‘fonts/akagi-th-webfont.eot?#iefix’) format(’embedded-opentype’), asset_url(‘fonts/akagi-th-webfont.woff’) format(‘woff’), asset_url(‘fonts/akagi-th-webfont.ttf’) format(‘truetype’), … Read more