Rails 4 Ticket Support System

“The result is an easy to use Rails 4 Ticket Support system.”

As we were soon going live with AwesomeWeb’s freelance marketplace, we were in need of a ticket support system.

At that time, I had to come up with something quick, convenient and friendly to use. I looked around of what other websites use and all I could see were 3rd party services with complex ticket support systems. I was not satisfied from such solutions and wanted to build something that will be on-site. Then I remembered about Digital Ocean’s ticket system. I used it couple of times in the past and it was really easy to use and straight to the point. After reviewing it a bit I decided to give it a try.

The result is a convenient, easy to use Rails 4 Ticket Support system that can be found in my github account. Feel free to fork/clone it and use it. Feel free to report any bugs/issues and improve it as much as you desire.

Make sure you read the README.md for the instructions, features and some special notices!

Devise error: email can’t be blank

Devise error: email can’t be blank? – Well it isn’t!

Many of you might have been frustrated with the Devise error: email can’t be blank.

You do indeed type an email in the text field but still get this error! I even took out the :validatable module from devise in order to get rid of this error. However that is not the solution.

The Rails 4 uses strong-parameters is causing this issue and the solution is simple

The solution is simple. You just have to place this code segment into your Application Controller (application_controller.rb):

  before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }

Keep in mind to configure for each action like :sign_in, :sign_up, :account_update etc by doing something like: (if it is of course needed for the other actions as well)

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation) }
  devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email, :password, :password_confirmation) }

Rails 4 Static Pages – How To

Creating Rails 4 Static Pages is really straightforward.

We all at some point needed to create a static page like an About or Contact us page. Rails 4 Static Pages are easy to create and the procedure is slightly different than Rails 3.

In Rails 3 you had to use the match method like:

match ':action' => 'static#:action'

However in Rails 4 the Static Pages can be created by using:

get ':action' => 'static#:action'

By doing that you have setup your Static’s page route. However you also need to create your static_controller.rb and input your “action” there. After that you need to create the app/views/static folder and make an action.html.erb file.

Your Rails 4 Static Page is now ready and visible when you browse to http://localhost:3000/action

Accidentally typed password into terminal?

Have you accidentally typed password into terminal? Don’t worry!

The probabilities are that this happened to you already. When you type your password then it gets recorded into bash’s command line history. This is a serious security vulnerability and you should immediately take action!

These are some of the quick fixes I suggest:

  • Change your password. If you do not have this privilege or you do not prefer to change your password you can…
  • Delete your typed password from command line’s history. To do so, input history into the command line. The commands history will show up. Take a note of the line you are interested to delete and input “history -d [line_number]”. Then type “history -w” to save the changes you made to the history.

Tracking views in Rails

Tracking views in Rails is very simple!

Some weeks ago I was asked to implement a feature that will be tracking the users’ profile views. As I come from a background of Java and PHP I was thinking of ways to efficiently implement something like that and prevent any database performance issues in future.

I decided to make it as simple as possible and then improve from there. Just after some minutes, and with some Ruby magic, I had it working!

Tracking views in Rails is really simple. Feel free to checkout my implementation on Github.

You can also use this for tracking views/impressions for Articles or Posts.

Ready to Release a Rails App?

“So, you are ready to release a Rails app? Awesome!”

When you are ready to release a Rails app (or your team’s app), there will be many tasks you need to handle and plan for. The World Wide Web is a “hostile” environment. As a result, when you release your Rails App you will face a load of different issues such as bugs, vulnerabilities, security flaws, performance issues, errors, warnings, lack of data analysis and monitoring and lastly difficulties to debug and respond to all these!

Gladly, nowadays, there are solutions out there that can help you respond and give you an edge at tackling all these issues.

First of all, I suggest that, before releasing your app you should get your Application’s code reviewed. You can either hire an experienced Senior Rails engineer (or a team) to go through your Rails App’s code or use a cheap and quick automated tool like Pullreview.

NewRelic is an excellent tool for Application Performance Management & Monitoring. It is relatively complex in comparison to other tools but it is a professional full scale platform. I personally use it to monitor my server’s CPU, memory stats, database and other website related stats such as page load time, errors, etc). In that way I can respond to potential performance issues or bottlenecks.

As we use Ruby on Rails, gemnasium is an excellent choice for monitoring vulnerabilities of our App’s gems and their dependencies.

Sentry and Airbrake are excellent tools that keep track of exceptions generated on your website by users. In that way you will be able to spot bugs and debug faster and easier. Also, it will be awful and time consuming to try searching your logs in order to find errors.

And lastly, you should keep track of the website users’ behaviour and features’ utilisation. Google Analytics with custom events would be sufficient but I also recommend that you check out Segment.io.

And now… You are ready to release a Rails App! :)

Gemnasium Review

“Gemnasium; an automated tool for tracking your dependencies.”

Our team is ready to deploy our Rails application. When a Rails application reaches this stage there are security concerns that should be addressed. Rails apps use gems as are sometimes required and sometimes beneficial for saving time and having a high quality implementation. However all these gems are consisted by dependencies which can introduce vulnerabilities. It is difficult to track these vulnerabilities as are many in number and a time consuming process.

Gemnasium (website) comes in and gives an efficient solution for this task. By subscribing to this service, your Rails application dependencies are tracked. When a new vulnerability is found you are notified via an email.

We have been using Gemnasium lately and had a very pleasant experience with it. In two different cases we received alerts on our email. The friendly interface makes it easy to identify which gem needs to be updated. Then, its just a matter of changing your gem’s version in your Gemfile.

The alerts are in real-time and you can also add several members on your Gemnasium account. As a result, your whole team can receive the vulnerability alerts and fix the issues immediately.

Rails 4 Devise Username

“Some users prefer username authentication but some email – Rails 4 Devise Username”

You desire to create the ability for your user’s to login with either their Username or Email. Luckily this task is really simple in Rails 4 Devise Username.

First of all make sure that you have the “username” attribute in your Users database table. Then in your view – where the user signs in – (app/views/devise/sessions/new.html.erb) replace the email field with:

<%= f.label "Username or Email" %>
<%= f.text_field :login %><br>

The parameter “login” will be used to later describe either the user’s email or username. In the user.rb model you need to make the following additions:

attr_accessor :login

def login=(login)
  @login = login

def login
  @login || self.username || self.email

def self.find_first_by_auth_conditions(warden_conditions)
  conditions = warden_conditions.dup
  if login = conditions.delete(:login)
    where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first

With these changes we assign the login parameter to the desired value and determine if the login value used was an email or a username.

As we use Devise, there is a need to permit the login parameter on Devise’s end. Also don’t forget to change Devise’s default authentication_keys from email to login.

In the app/controllers/application_controller.rb under the def update_sanitized_params method we should add login for sign up:

devise_parameter_sanitizer.for(:sign_in) {|u| u.permit(:login, :username, :email, :password, :remember_me)}

and in config/initializers/devise.rb, we should change the “config.authentication_keys = [ :email ]” to “config.authentication_keys = [ :login ]”

That’s all! If you have any difficulties or feedback feel free to contact me or leave a comment below.

Setting up Ruby on Rails Development Environment

“When it comes to Web Development via Ruby on Rails, I prefer working with the Mac OS X.”

OS X makes it so easy for setting up Ruby on Rails development environment; but I still observe other junior developers having troubles with setting it up.

Seven simple steps!

1. First of all, Mac OS X provides Xcode and Xcode Command Line Tools which are development tools. You can download them from the Apple Developer site.
2. Then, install homebrew: ruby -e “$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)“. After the installation, run “brew doctor” to make sure homebrew works fine. If not, follow the instructions of the message that appears.
3. Install GCC 4.2 by inputting “brew install apple-gcc42” & “sudo ln -s /usr/local/bin/gcc-4.2 /usr/bin/gcc-4.2“.
4. Install RVM with “curl -L https://get.rvm.io | bash -s stable –autolibs=enabled
5. Install Ruby 2.0.0: “rvm install 2.0.0 –default” (or any other version: “rvm install “version”).
6. Lastly, install Rails(and the bundler): “gem install rails bundler –no-ri –no-rdoc
7. Tip: If have any errors about missing libraries then just search on google about the libraries associated with the error and then execute: “brew install [library-name]

1. Will you be uploading your code to Github? Install git with: “brew install git
2. Will you be working with SQLite3? Install it with: “brew install sqlite3
3. Will you be working with MySQL? Install it with: “brew install mysql
4. Will you be working with PostgreSQL? Install it with: “brew install postgresql

Working with Timestamps

“Working with Timestamps is a usual task in Rails.”

Many times you find yourself in need of a friendly date and time format. Luckily, Rails’ Helper methods provide many different ways to make this possible. A timestamp in Rails is shown like: “2014-01-17 23:36:54 +0000”. So what if we want to present the time that a blog post was created in a friendly format? For example: “This post was created on the 17th Jan, 2014 at 23:36”?

For grabbing the year, month and day we can call “post.created_at.year”, “post.created_at.month” and “post.created_at.mday” respectively. For grabbing the time we can call “post.created_at.strftime((“%H:%M”)”.

Note that all these variables will return numbers. However, in our example we need our month to be the abbreviation of January. For doing so, we just use the I18n ( Rails Internationalization API ) with a code like: “I18n.t(“date.abbr_month_names”)[post.created_at.month]”.

So, working with timestamps in order to show “This post was created on the 17 of Jan, 2014 at 23:36”, we will use this in our view:

This post was created on the <%= post.created_at.mday %> of <%= I18n.t("date.abbr_month_names")[post.created_at.month] %>, <%= post.created_at.year %> at post.created_at.strftime(("%H:%M")

Another great way to present time in a friendly format is by using “time_ago_in_words(post.created_at)” which will render a format like: 3 days ago or 15 minutes ago.