Jump to content

The ultimate community for Ruby on Rails developers.

Most Liked Content

#4 Welcome to the new Rails Forum

Posted by Adam on 14 August 2013 - 04:15 PM

When we launched Rails Forum in May 2006, Rails was just barely past 1.0. It was a fledgling framework with a head of steam, but not much widespread adoption. There weren't too many places to go for help outside of the official email lists -- even Stack Overflow was still two years away!


Once we opened the virtual doors to the forum, it was obvious immediately that it was filling a real need. An active community sprang up almost overnight, and grew to over 4,000 members and 24,000 posts in the first year. As Rails grew, so did the forum. 


Unfortunately, we didn't do a very good job of maintaining that community and it has been overrun by spam in recent years. That's unacceptable and we apologize for letting it happen. 


That's why today we're happy to announce that we're relaunching Rails Forum. We want the forum to get back to its roots. We want to be a useful resource that contributes positively to the Rails community again. We needed a clean slate. To that end, we've ported everything over to a more mature and secure software system, redesigned the site, and put in place a new moderation team.


Please pop in into the Introductions forum to say hello :)


We hope you'll join us and register for the new Rails Forum! Please don't hesitate to let us know if you have concerns or suggestions for serving you better.

  • midwire, kjetil, Jamie and 1 other like this

#132 CentOS vs Ubuntu Server

Posted by Tom on 14 August 2013 - 08:59 PM

BTW this question is why it's good to have a RailsForum. This question wouldn't survive 60 seconds without being closed on StackOverflow.

  • AstonJ, MrPepper, burmjohn and 1 other like this

#71263 Moderators Needed!

Posted by Adam on 24 February 2015 - 11:49 AM

Hi there,


We're looking some moderators to join the forum and help out with keeping SPAM at bay. If you've been around for a while and would be happy to delete a few SPAM posts when you're passing, that'd be great.


Please drop me an message on here if you'd like to help out :)


Many thanks,



  • jefflunt, Ohm and RubyNewbie like this

#6 Setting up desktop notifications with Rails & Noti

Posted by Adam on 14 August 2013 - 04:21 PM

This tutorial will guide you through how to set up desktop notifications within a Ruby on Rails application using Noti.


Noti is a web service which allows users to receive desktop notifications from any online application without the need for their browser to be open or them to be actively visiting your website. Any user who wishes to receive notifications will install a tiny native OS X, Windows or Linux desktop application and login using their own Noti username & password


Before your application can send notifications to a user you will need to ask them to "authorise" your application. This authorisation process will provide you with a unique token which you can use as an address when sending your notifications to their desktops.


Configuring for battle


Noti provides an easy to use RubyGem which provides all the core functionality you need to get started. To begin with we will need to load this gem into our Rails application's Gemfile. Open up your Gemfile and add the following line to the bottom:

gem 'noti'

Once you have added this and saved the file, you should run bundle install to install the dependency. If you haven't already signed up for a Noti account you should do this now and create a new application within the Noti website. When you have created this application, you will be provided with a token which you need to add into config/initializers/noti.rbas so:

Noti.app = '4842e7ad-1cc5-66bf-8e4b-a9ec7ac3b828'

Be sure you replace the token above with your own application's token. Once you've done this, be sure to restart your web server processes.


Get authorisation from your users


We will assume your application has a User model which you use to identify users who login to your application. It will be this model which stores the user's Noti "token". You will need to add a new string-formatted column to your users table called noti_token.


Now we need to populate this field with a token from the user. In order to do this, we must provide a link in our application so that our users can authorise our application to send them notifications. We will first create a new controller in our application which will be responsible for handling all of these authorisation actions.

rails generate controller NotiAuthorisations

Now we will create some routes in our config/routes.rb file which will point to our new controller. Open up this file and add the following 2 routes.

Notitest::Application.routes.draw do
  post 'noti' => 'noti_authorisations#new', :as => 'setup_noti'
  get 'noti/callback' => 'noti_authorisations#create', :as => 'noti_callback'

Now these routes are in place, we will go ahead and add some code to our new controller. Open up your newly created app/controllers/noti_authorisations_controller.rb file. This controller will consist of two methods:

  • new - this will initiate a new authorisation request. If a user wishes to authorise your application, you will send them to this action.
  • create - once a user has authorised your application with the Noti service, they will be returned to this action so we can finalise things.
class NotiAuthorisationsController < ApplicationController
  def new
    token = Noti::Token.create_request_token(noti_callback_url)
    session[:noti_rt] = token.request_token
    redirect_to token.redirect_url

  def create
    token = Noti::Token.get_access_token(session[:noti_rt])
    current_user.update_attribute(:noti_token, token)
    session[:noti_rt] = nil
    redirect_to root_path, :flash => "Noti has now been configured for your account!"

Now, lots of things happened there. Let's firstly look at the new method. This firstly requests a "request token" from the Noti API by sending a URL where the user should be returned to after they authorise your app. This returns an object which implemented two methods: request_token which contains the randomly generated token for this specific authorisation and redirect_url which is the URL which you must redirect the user to complete the authorisation. We use the first of these methods to set a session variable called :noti_rt and the second to redirect the user to the Noti website.


When the user is redirected they will be shown a message saying that you are requesting permission to send them notifications. They have the option to login or sign up for an account. Once they have completed one of these actions they will be redirected back to the URL specified when you generated the request token. If they sign up for an account, they will also be sent an email outlining how they should go about downloading the client software.


When the user is redirected, they will be sent to the create action we just wrote. This method uses the session variable we created earlier to ask Noti for a corresponding "access token" to go with the "request token". The token provided will be unique to the individual user and this must be stored along with their other information. This is the unique token used to send this user desktop notifications. This method now assumes that you have a current_user ActiveRecord instance for the currently logged in user and uses the update_attribute method to permanently store the user's Noti token. The final part of this action clears the session variable and redirects the user with a message alerting them to the fact their Noti account is now configured.


Of course, in order to start the authorisation process you will need to provide your users with a link to the 'new' action which we defined above. You can do this using a simple link_to method in one of your views:

link_to('Setup Desktop Notifications', setup_noti_path, :method => :post)

Finally, let's send a notification!


Now we have user's Noti tokens safely stored in our database, we can send them notifications! Yay!

Sending a notification is a very simple process and allows you as the application developer to choose a title, additional text, a sound as well an image to display with the notification. You can choose to send notifications from anywhere in your application but for this example, we will extend our NotiAuthorisationsController#create action to send a "welcome" notification when the user logs in.

notification = Noti::Notification.new
notification.title = "Notifications are ready to go!"
notification.text = "You will now receive application notifications directly to your desktop"
notification.sound = 'alert2'

The last code block is pretty much all you need to know about sending notifications. Once you have created a notification object, you can use the deliver_to method to send the notification itself. This method accepts a user's Noti token as it's only parameter.


You may be wondering what sounds are available? A full list of sounds can be found in the Noti API documentation. You're probably best just experimenting to hear what each of these sound like.


And there you have it!


Congratulations - you now have desktop notifications in your web application! You may want to explore additional features like bulk-sending notifications and obtaining notification display statuses. Full details about these can be found in the Noti API docs or on the gem's Github page.

  • shorepound, CapRoR and Tom like this

#558 Best way to learn Rails?

Posted by AstonJ on 17 August 2013 - 10:23 PM

When I first started out, I asked that question a lot. I just wanted someone to say, look do this this and this, then this this and this - a bit like a step-by-step, but one that would work. I didn't find one, but did get to speak to lots of prominent Rails folk and took on-board their advice... and promised myself I would do such a guide for anyone else serious about learning Ruby and Rails.


So here it is: Best way to learn Rails I hope you enjoy it as much as I did!


(I'm not sure if it still is, but it used to be returned with the !Learn factoid in the official Rails chat room - just adding that in case you're wondering whether it's a good guide to follow or not)


Please feel free to add your own tips on learning Rails :)

  • Tom, MrPepper and patrickmaciel like this

#261 Tapatalk Support

Posted by Adam on 15 August 2013 - 10:59 AM

We've now added this :)



  • Funnyvibe, Jamie and burmjohn like this

#225 Changes in Rails 4

Posted by Andre Dublin on 15 August 2013 - 02:10 AM

I've always liked using http://railsdiff.org/

  • BrockFredin, jefflunt and wilsonsilva like this

#1167 RSpec and using --no-test-framework?

Posted by Kelli Shaver on 09 September 2013 - 08:49 AM

I think writing tests could be useful, but probably not TDD for the reasons you mentioned (is it the app code, or is it the test that's breaking?). Code something, test it in your browser and see that it works, then write a test for it. I feel like it's a practical way to learn the workings of your testing framework of choice. 


Knowing how/what to test for and what makes a good test is another matter entirely. Check out http://betterspecs.org - there's some really good info there. 

  • Rowel, rayray and orrymr like this

#1003 How to add additional attributes to the join table in HABTM and fetch that wh...

Posted by Rowel on 03 September 2013 - 11:50 AM

And consider i need to add a field say type in the join table along with the reference where i need to add the type of the category.


I think you need to use has many :through  relationships instead of has_and_belongs_to_many  for your particular situation, since you need to also work on their relationship table.   




  • Kelli Shaver, james and Logesh like this

#72497 Adding a new field to a form

Posted by rahoulb on 06 August 2015 - 02:24 PM

One of the ways that Rails saves you time, compared to the frameworks that went before it, is that it reads your database and then makes decisions on what attributes you need based on that.  


The db/schema.rb is a file that shows what Rails thinks current database schema is.  If you edit it by hand, you've not actually changed the actual database (Mysql, Postgres, SQLite), all you've done is edit a file and made it not match what the database actually contains.  


So your database still doesn't have an "about" field, and therefore it can't figure out what to do when displaying the field.  


An ActiveRecord migration is a series of commands that connect to the database, change the schema IN THE DATABASE itself, then update the schema.rb with the current contents of that database.  


You will need to generate a migration (using the `rails generate migration` command) and then edit it so that it adds a column to the profiles table (`add_column :profiles, :about, :text`) and then run that migration `rake db:migrate`.  You don't need to precompile the assets (most people never need to precompile assets; it's normally done for you when you deploy the app to live servers).  


It may be worth spending some time reading through the Rails guides (migrations are here: http://guides.rubyon...igrations.html)and I would recommend looking at a good tutorial for getting the hang of some of the basics (the Michael Hartl one comes highly recommended although I've not tried it).  

  • ns_sg and folubode like this

#72128 Sub-forum README

Posted by jefflunt on 11 May 2015 - 04:07 PM

I'm a fan of minimal rules, so I'll keep this short. If you'd like to discuss these rules head over to the meta sub-forum, that's what it's for.  :)


What this sub-forum is for:

  • Building a community about Rails!
  • Talking about the cool stuff you just built, and trying to get people interested in it (just make sure not to cross the line into spammer. :))
  • Discussing Rails as a framework, Ruby as a language, and associated tools (gems, database engines, your favorite templating language - oh, but don't discuss editors/IDEs, vim is obviously the best :ph34r:, so it's pointless to bring up the topic again and again)
  • Asking for recommendations + suggestions that don't fall into the "What this sub-forum is not for" section.

What this sub-forum is not for:

  • Asking for help or for someone to debug/examine/critique your code/config file/server deployment, etc. Except for very rare exceptions (which are so rare that it's hard to even articulate them right now) those questions should go to one of the following amazing StackExchange network sites:
    • StackOverflow for programming questions and all things Rails + Ruby
    • CodeReview if you're looking for someone to critique your code, such as feedback regarding design, organization, efficiency, style, etc.
    • ServerFault if you need help with various non-Rails/non-Ruby tools such as Apache/nginx configuration, Linux and other OS questions
    • I know there are a lot of Q&A posts in this sub-forum already, but consider them deprecated, present only because of legacy support, but not officially supported going forward.
  • Job posts:
    • This isn't a job board. There are job boards for that sort of thing, such as r/railsjobsStackOverflow Careers, or WeWorkRemotely.
    • Don't post looking for a job.
    • Don't post looking to hire someone.
    • I know, this moderator must be a real hard-nosed jerk! Well, actually I'm just trying to point you to the best tool for the job at hand: a job board.
    • But why? ​Job posts tend to show up on community sites of all kinds for two main reasons:
      • Developers don't realize how much more efficient job boards are at this kind of thing, and should be kindly directed to go to said job boards.
      • Companies want a free way to advertise their job posting, because it typically costs them money to post jobs on job boards, but they think:
        "Hey, I could just spam the crap out of this board for free and maybe snag a decent developer in the process. What's the downside?"


        Look, companies are companies, and they like free stuff just like you. But here's the thing: companies have money for these things (I should know, I'm a business owner myself) and companies should be expected to pay in order to distract you with attention-grabbing ads (which is what job postings are). So, don't feel pity for companies. They are not human. They are money-producing superbots hell bent on taking over the planet and all its resources, wanting nothing more than the pesky humans to get out of the way. Don't pay them mind in this community as they roll their tank tracks over your face. The companies will feel nothing.

        Besides, we'll need a place to organize our anti-superbot rebellion, and if we let them infiltrate the forum now then we've already lost!



If you're not sure which sub-forum your post belongs in:


Just pick one. What's the worst that could happen, a moderator moves your post? Not a big deal, right? Just don't cross-post across multiple sub-forums for goodness sake! Like Ruby: DRY (don't repeat yourself) and all will be well.





  • Ohm and folubode like this

#72118 Setting Up Production Server on VPS

Posted by jefflunt on 10 May 2015 - 11:33 PM

There's this old video, which still basically works: RailsCasts episode 335 (I'm sure there are newer tutorials out there, but this one will be fine to learn on)


Watch that video, then look at the broader picture. All production VPS Rails servers have the following roles:

  • A web server such as nginx or apache
    • receives the incoming requests on port 80 or 443 (HTTP / HTTPS)
    • passes the content of the request on to the app server, and awaits a response
    • when the app server responds, the web server passes the results back to the client (typically a browser)
  • An app server such as unicorn or passenger
    • runs your actual web app
    • receives request from the web server, processing it via the Rails app you've built
    • returns a result back to the web server

Webrick is a combined web+app server. It's a quick and dirty solution for development that works and doesn't require you to learn that much to get started. Technically you could run your production site using it, and indeed people used to do that, but there are better, more specialized tools these days. The only real difference in production is that the web server (which handles network communication) and the app server (which runs your ruby code - the app) is split into two pieces instead of one.


Obviously you'll need a DB server in there as well to store your data, but that works pretty much the same in development and production.

  • drdan and folubode like this

#6358 How to select all users except ids

Posted by Ohm on 27 October 2014 - 10:27 PM

If you have all the ids of the users you don't want in params[:ids] just use that

User.where.not(id: params[:ids])

  • pramod and Pinta777 like this

#6327 Time.now returns the server time or client time ?

Posted by Ohm on 15 October 2014 - 05:55 PM

Try it out. Launch a Rails console on the server and type:


You'll get a result back like:

=> 2014-10-15 19:55:29 +0200

If you have configured your Rails app to a different timezone than the one it's located in (if you have e.g. an European app running on an server in the US) you'll need to use:


which will return a time like:

=> Wed, 15 Oct 2014 19:55:32 CEST +02:00
So, to answer the question: You'll (of course) get the server time back when running Time.now on the server.

  • Anuj Dubey and Pinta777 like this

#5685 How to make a connection between the models?

Posted by Ohm on 11 September 2014 - 08:23 PM

I think what you're looking for is single table inheritance (STI) which lets you create the following:

class Item < ActiveRecord::Base
  has_many :switches

class Switch < ActiveRecord::Base
  belongs_to :item

class Photo < Switch

class Store < Switch

If the Switch model has a type column, it'll use this to figure out which of the two Photo or Store that it'll use.


A normal use case would then be to add scopes to the Switch model:

scope :photos, -> { where(type: 'photo') }
scope :stores, -> { where(type: 'store') }

and then delegate to these scopes from the Item model:

delegate :photos, :stores, to: :assets

  • eViper and Anuj Dubey like this

#4114 adding searchablecolumns

Posted by Ohm on 26 July 2014 - 09:20 PM

You need to write the correct SQL for searching either in item_code or colour_description, using SQLs OR.


This can be done like so:

def self.search(search)
  if search
    where('item_code ILIKE :search OR colour_description LIKE :search', search: "%#{search}%")

  • Jamie and DvDj like this

#4080 link_to with array parameter for URL : What does it mean?

Posted by Clint Bishop on 21 July 2014 - 02:47 AM



The short answer to the question is: an array containing an Article of id 1 and a comment of id 2 will translate into this URL: /articles/1/comments/2.


I wrote a little more on the subject here: http://norobotdiving..._to-with-array/. Check it out!

  • Jamie and rovf like this

#3943 Advanced Tutorials

Posted by Jimbollu on 16 June 2014 - 01:31 PM

Hi All. I think I know pretty much everything there is to know about how to use Rails. 100% of the terminology. 


However, I struggle when it comes to making more complex sites. Why? Because the uncountable billions of tutorials I've watched/read are pretty basic, and all of them only briefly touch on ajax. 


If I were to read some advanced tutorials, I'm sure all of my knowledge would fall into place. However I can't find any! They're all basic, all introductory. Looking at the contents pages of so called 'advanced' books seem to imply the 'advanced' tutorials cover RESTful architecture and MVC, just stuff I know in my sleep.


I love learning from actual projects. Learning terminology or what aspects of rails are in isolation just doesn't help me at all. 


I want a huge, detailed tutorial on

  • How to build a forum
  • '' a social network (a real deal facebook-esque one)
  • '' videosharing site
  • An upload manager with upload bar (like on this site)
  • '' a file conversion site (Although maybe Rails isn't the framework for that)
  • " A github clone. (Gitlab is already on RoR)
  • " A CMS
  • ... as well as vast and intricate control panels for all of these things. A control panel like this:

Screenshot from 2014-06-16 14:10:33.png


I assume we'd use AJAX requests and JSON routes to retrieve this information, but I've yet to see a tutorial, and I have no idea how the javascript would look. And I'm fed up of using my intuition only to realise I've wasted weeks and I've been barking up the wrong tree. I mean I say AJAX and JSON, but maybe websockets should be used instead. I need guidance on how to use my knowledge, not pure knowledge.


Some of this may be more Javascript rather than Rails, but even so Rails is deeply involved in all of my ideas and I can't believe how sparse the resources are out there. 


This site:




Appears to use some ungodly union of ASP and Flash (based on the URL, nothing more), but I'd love to be able to create something like this with Rails and AJAX (only as a practise project). I'm sure I could give it a very good try, but I'm concerned that by the end I'll either have a poorly-organised project / halfway through I'll have to scrap everything because I've realised there's a far better way to do things.


Apologies if I sound annoyed it's just I've been the last 4 months looking for advanced tutorials and just now I've just snapped because there is NOTHING. 

  • Jimbollu and ahnbizcad like this

#3777 One-line loop in a view

Posted by Ohm on 15 May 2014 - 11:12 AM

If what you really wanted was 01234567, you could do it like this

<%= 8.times.to_a.join %>

if you want the space between them supply the #join method with a space.

  • Jamie and arnig1981 like this

#21064 PHP Mobile phone SMS payment system in Ruby on Rails

Posted by Ohm on 24 November 2014 - 09:16 PM

I'm sorry to have to do this, but:



  • Linards and zachariastr like this