Skip to content

Instantly share code, notes, and snippets.

Images not working in Rails 4 -- Ruby brackets still in #main after bundle exec rake assets:precompile RAILS_ENV=production according to Chrome dev tools.

test.css.erb

#main { background-image: url(<%= asset_path 'main.png' %>); }

application.html.erb

Detecting mobile devices and tablets in 2014

The mobile world changes almost daily. Expanding on http://scottwb.com/blog/2012/02/23/a-better-way-to-add-mobile-pages-to-a-rails-site/ -- which is sorta old -- what do you guys think of this mobile and tablet detection scheme?

Use case: The mobile and desktop versions of my Rails app are very different. Although they share the majority of views, JS and CSS (using media queries where applicable), there are cases where I need mobile-only and desktop-only views, JS and CSS.

Code changes

All tablets should be treated as mobile.

unless user.nil?
forum = Forem::Forum.find_or_create_by_name(:category_id => Forem::Category.first.id, :name => "Default", :description => "Default forem created by install")
#########
topic1 = forum.topics.build({ :subject => "Test", :posts_attributes => [:text => "Hello World"] })
topic1.user = user
topic1.save!
#########

:paragraph not behaving as expected

This user input:

Hello,

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
% rake db:migrate
[...snip...]
% rake db:seed
rake aborted!
undefined method `id' for nil:NilClass
/home/dev/demo-search/db/seeds.rb:16:in `<top (required)>'
/home/dev/.rvm/gems/ruby-1.9.3-p448@brgen/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
/home/dev/.rvm/gems/ruby-1.9.3-p448@brgen/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
/home/dev/.rvm/gems/ruby-1.9.3-p448@brgen/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'

Rails 3

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
NameError in Forem::Forums#index
Showing /home/dev/demo-search/app/views/layouts/application.html.erb where line #23 raised:
undefined local variable or method `search_path' for #<#<Class:0x8a5b79f0>:0x8a5b70f4>
Extracted source (around line #23):
20
21
22
23
24
# uname -a
Linux myapp 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

# pumactl -v
2.6.0

# pumactl -S /home/myapp/tmp/puma.state restart --config /home/myapp/config/puma.rb
Command restart sent success
{
"ItemSearchResponse" => {
"xmlns" => "http://webservices.amazon.com/AWSECommerceService/2011-08-01", "OperationRequest" => {
"HTTPHeaders" => {
"Header" => {
"Name" => "UserAgent", "Value" => "Jeff/1.0.1 (Language=Ruby; dev.my.domain)"
}
}, "RequestId" => "4ba63efd-0ff2-4b71-9713-eced654944fd", "Arguments" => {
"Argument" => [{
"Name" => "Service", "Value" => "AWSECommerceService"