rails – uninitialized constant dashboardcontroller
This is a quick hint:
If you play with ActiveAdmin and get the error:
“uninitialized constant dashboardcontroller”
open your routes.rb in your /config-folder. Now look for:
1 | root :to => 'your_controller#index' |
You have to be sure, that your root definition is before
1 2 | devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) |
So your routes.rb could look like
1 2 3 4 5 | YOUR_APP::Application.routes.draw do root :to => 'your_controller#index' devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) |
Restart your server and have fun
Project Euler – problem 2 in Ruby
Hi folks,
this is part two of our soultions-series for the mathematical problems in Project Euler.
Project Euler – problem number two:
“Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.”
Here we go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # first we set our limit to four million limit = 4000000 # let's create an array with our start values start = [1,2] # this creates a block with our added values, until # we reach our limit (four million) while start[-2] + start[-1] < limit start << start[-2] + start[-1] end # we set result to zero result = 0 # we need only values added, which are even start.each do |x| result = result + x if x.even? end # print our result puts result |
Please note that you can write this code much more elegant. I did not for learning purposes.
This here
1 | result = result + x if x.even? |
becomes
1 | result += x if x.even? |
or this
1 2 3 | start.each do |x| result = result + x if x.even? end |
becomes
1 | start.each { |x| result+= x if x.even? } |
and so on ….
Project Euler – mathematical problem 1
I recently heared about “Project Euler” from a friend. For all of those who have not heared about this project:
“Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve.”
The first mathematical problem to solve was:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
My solution will ask also for a number, so you can scale that problem to extreme values ;-)
Please note: You can surely write this 4 times shorter in ruby than my explained solution – BUT(!) for learning purposes I wanted a solution which also explains everything well for you!
Here we go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # first, everything is zero ;-) counter = 0 # ask for an end-digit and convert it into an integer end_digit = gets.to_i # range from 1 to our end-digit and put it into a block (1...end_digit).each do |x| # now let's have a look, if we can find numbers with a # modulo function of three and five counter = counter + x if (x % 3 == 0) || (x % 5 == 0) end # print out the solution puts counter |
Just copy and paste it into a file and run the file in a shell, enter a value and enjoy.
Have fun while playing with values…
Ruby 2.0 encoding is utf-8
As I wrote in my article about Encoding, Ruby methods dealing with encodings return or accept Encoding instances as arguments (when a method accepts an Encoding instance as an argument, it can be passed an Encoding name or alias instead).
Normally, the default script encoding is Encoding::US-ASCII, but it can be changed by a magic comment on the first line of the source code file (or also the second line, if there is a shebang line on the first). The comment must contain the word coding or encoding, followed by a colon, space and the Encoding name or alias.
In Ruby 2.0 we do not need magic comment in case the encoding is utf-8.
1 2 3 4 5 6 7 | # Ruby 1.9: # encoding: utf-8 # ^^^ previous line was needed! puts "❤ Schöne Überraschung ❤" # Ruby 2.0: puts "❤ Schöne Überraschung ❤" |
Now enjoy Ruby 2.0!
Chip Online – Ruby on Rails book review
Happy new 2013!

Because the world didn’t end in 2012 (you know, Maya calendar and conspiracy …) I had a bit time in my holidays over Xmas and wrote an article at http://blog.chip.de/itrezensionen.
For those who never heard about this site:
You can find there tons of reviews for IT books. Whenever you want to know if a book is good or dumb, check this site. The reviews are according to the motto actuality, independence, reader oriented. This site is under the brand of CHIP Xonio Online GmbH (www.chip.de) and is brand leader in germany.
My review was for a german Rails Book from Galileo Verlag called “Ruby on Rails 3.1″. If you can master the German language, please have a look and enjoy reading on at http://blog.chip.de/itrezensionen/rezension-ruby-on-rails-3-1-20130110/
Have a good start in 2013!
Marcello
Rails input size is 30 chars
Just a quick fix, but leads often to confusion …
Let’s say we have a form
1 | <%= form_for link , url:link_path(link) , remote:true do |f| -%> |
now we want to insert an input field like
1 | <%= f.text_field :email, placeholder:'Email', :title => 'email' %> |
this will will give you a HTML like this
1 | <input type="text" title="email" placeholder="Email" name="user[email]" size="30" id="user_email"> |
What scares me is the fix size of an input field of 30 chars. This is Rails standard. When you have a fluid or mobile view, this will destroy your elegancy of the CSS grid. We could insert a size parameter in each of our input fields, but that would be very uncool ;-)
Just insert your Input field like this, and everything is fine:
1 | <%= f.text_field :email, placeholder:'Email', :title => 'email', :size => nil %> |
Watch the :size => nil in the input field.
Now mobile view is fine again…
Postgresql and TCP/IP connections on port 5432
When working with Postgresql, I got the error
1 2 3 4 | PG::Error could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? |
This can happen, if you didn’t configure your server specifically.
To see if your Postgresql is accepting requests, simply run the following command in your terminal:
1 | psql -h localhost mydb |
To resolve this issue, follow the next steps.
Continue with Postgresql and TCP/IP connections on port 5432 »
Ruby on Rails mobile view
Finally I had enough time at the weekend to care about my blog and the mobile views.
My blog now detects devices like iPhones, iPads, Android & and more, serving its optimized theme instead of my regular desktop theme. You can always switch back to standard/desktop theme if you wish (the button is in the footer).
For long code columns and tutorials, I suggest using your mobile device in landscape format.
It will be adjusted automatically to fit your screen. You can still use the zoom with your fingers, if the font is too small. Next step will be HTML5 and cleanup the frontend. It’s all about time … ;-)
Comments are welcome.





Invalid multibyte char (US-ASCII) Ruby
I was playing with Couchbase and plain Ruby in my Terminal. Then I got the error message:
If you got this error, you know that this is a result of having special chars. To fix this, you have to add a ‘magic comment’ in the script.
Here is a step-by-step instruction how to solve this problem:
1. Open your file
2. go to the VERY FIRST line of your code
3. write the following at the top of your file
2
# encoding: utf-8
What is the background, why is this happining?
All source files receive a US-ASCII Encoding, unless you say otherwise. If you place any non-ASCII content in a String literal without changing the source Encoding, Ruby will die with that error. With magic comments the code will include its Encoding data. It will probably seem a little tedious to add them to all your source files at first, but it’s really not that big of a change.
If you want to add magic comments on all the source files of a project easily, you do not have to follow this steps for each file. Better use the magic_encoding gem. Just enter in your console:
Then just call magic_encoding from the root of your app.
Let’s rock …
Tags: encoding, gem, magic comment, ruby