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


#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'
end

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
  end

  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!"
  end
end

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'
notification.deliver_to(current_user.noti_token)

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 :)

 

Enjoy.


  • 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


#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 and burmjohn 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.   

 

http://guides.rubyon...ion_basics.html

has_many_through.png


  • Kelli Shaver, james and Logesh like this


#909 Need help choosing a book to get started

Posted by Adam on 29 August 2013 - 03:55 PM

I'd recommend starting with this:

 

http://pragprog.com/...nt-with-rails-4


  • Andre Dublin and james like this


#828 Does rails console know the application?

Posted by Jamie on 26 August 2013 - 08:33 PM

No it doesn't. It just looks like it does. Seriously you need to get your head round associations and how they work otherwise you will keep running into these problems.

 

James is correct and makes sense. I would follow the advice he has given and read the Rails Guides on associations :)


  • james and Jarrod like this


#583 cannot load such file -- sqlite3/sqlite3_native (LoadError)

Posted by cicolink on 18 August 2013 - 06:46 PM

Ooook, 

 

anyway, I uninstalled ruby and all the gems, check all the libraries u said are installed, compiled ruby, make, make install: no errors nor warnings.

 

Installed rails using gem install rails.

 

Created a new rails project, it installed automatically the gems sqlite (1.3.8).

 

I had to add the line:

 

gem 'execjs'

gem 'therubyracer'

 

in my gemFile since I got an error about execjs... And run bundle install

 

..... aaaaaaannnnnd.....

 

IT WORKS!!!! :) 


  • Jamie and Tom like this


#564 Best way to learn Rails?

Posted by Funnyvibe on 18 August 2013 - 01:23 AM

I used the pragprog books mentioned in your article to learn both ruby and rails. I can attest that they are pretty much the best books out there for new developers that learn the best through performing the task. They remind me a lot of a book that I used way back in the day to learn basic, something to the effect of "learning basic through applications." if there's anything I can emphasize enough though, it's to learn to read and apply documentation. Rails is a great place to start for that. All in all great article and advice for newcomers.
  • AstonJ and james like this


#544 CentOS vs Ubuntu Server

Posted by Tom on 17 August 2013 - 04:17 PM

IMHO, I think Debian and Ubuntu are both great, Ubuntu got distribution updates more often which is not always cool for sysadmin. Debian have more outdated packages, but most major services (nginx, postgresql, etc) provides they own repository with the last versions.
 
I tried both and I'm now using debian 7.1 (without GUI, it's useless :)


A few clarifications:

Ubuntu has two tracks of releases: the regular releases and the Long-Term Support (LTS) ones. The regular releases are every 6 months and the LTS are every 2 years. Regular releases are supported for 9 months while LTS releases are supported for 5 years. If you are concerned about long term stability (like you might be with Rails) the LTS version is the way to go for a production environment. Of course you can upgrade the regular versions when they come out but you are risking introducing incompatabilities if you have a stable Rails app running.

Debian has three tracks: stable, testing, and unstable. "Debian" generally refers to stable. Every package goes from unstable to stable as it matures and receives updates. This may make stable packages seem "outdated" but in reality they are just much better tested. That's what makes Debian Stable so rock-solid (and along with RHEL/CentOS) one of the two defacto major Linux server distributions. (Ubuntu LTS comes in a distant third in terms of market share.) Unlike Ubuntu, Debian does not have strict release and support time-frames which some people like and some don't.

Ubuntu *is* a snapshot of Debian testing with some modifications and rebranding. This makes Ubuntu less stable than Debian but it allows the packages to be based on newer versions of software. The Ubuntu view is that Debian testing (with some minor mods) is good enough in exchange for getting slightly newer versions of software. Like time-frames, this is an area where people have to make their own judgements.

Even though the two are very similar, one should never assume he or she should use the same track on their desktop and server. The server should be stable and well supported. It usually only needs to run a small set of software very well for long periods of time. A desktop on the other hand has to run many types of software for short periods of time. A desktop does not have to be as stable and it's very common to use Ubuntu's regular releases or Debian testing (or even unstable) for a desktop.

Finally, GUIs are meant for desktop use or for very inexperienced server admins (I say "admins" with hesitation because you cannot administer many required things without the command line.). Everything can and should be done on the command line as GUIs add a lot of unnecessary overhead to a server. Also, GUIs seldom cover every intricate feature that can only be accessed on the command line. It may take some time to get comfortable on the command line but it is essential if you are running production servers.
  • james and iMatt like this


#3069 Rails installation on windows

Posted by jack on 24 February 2014 - 09:45 AM

Mint with Cinnamon is wonderful. Personally I still use Ubuntu, but I have no real reason to prefer either. It is very rare that I ever need the GUI, as most of what I do is terminal based.


  • james and dailammoc like this


#2418 Add Ruby and make it default code highlighting language

Posted by Ohm on 22 December 2013 - 08:10 PM

When I use the code block it makes code like

this

however I can only choose PHP, Javascript, HTML, SQL, CSS, XML, and none. Why can't I choose Ruby? And why aren't Ruby the default?


  • jack and james like this


#1839 newbie question

Posted by Ohm on 31 October 2013 - 06:11 AM

Variables with a : before them are symbols

:symbol

Think of these like constants.

 

When working with hashes, you'll see stuff like

hash = { :key => 'value' }

Here the symbols are used as keys. In Ruby 1.9 they introduced a shorthand for the above, namely

hash = { key: 'value' }

which is the same way Javascript defines objects and hashes.


  • james and Rowel like this


#1647 Retrieving a random record - any other suggestion?

Posted by Ohm on 15 October 2013 - 05:31 AM

You should do something like

c = Bibleverse.count
Bibleverse.find(:first, :offset => rand(c))

as it will be a lot faster on big tables. Note that this is two SQL queries instead of one.

 

If you want to use your approach, you do not need limit(1) with first, as first already puts a limit(1) on an ActiveRelation.


  • katafrakt and Bharat Soni like this


#1413 Current Week Days Display

Posted by Ohm on 25 September 2013 - 07:34 PM

Doing 

(Date.today.beginning_of_week..Date.today.end_of_week).map {|w| w.strftime("%A")}

will give you

=> ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

If instead you'd rather like Mon, Tue and so on, replace the "%A" with "%a":

project> (Date.today.beginning_of_week..Date.today.end_of_week).map {|w| w.strftime("%a")}
=> ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

  • james and MrPepper like this


#1401 Start study Rails 4 or 3.2? What better for now?

Posted by stevieing on 24 September 2013 - 10:51 AM

If you want to future proof your apps then it is probably better to move to Rails 4.

 

If you use a VPS the choice of version is up to you.

 

If you want to know the differences between 3 and 4 then it is best to do some research. Ryan Bates has done some tutorials at railscasts.com. The RoR wesbite also has some details and the api will give you further information.

 

When you do gem install rails on any system it will install the latest version. If you want to manage your Rails versions easily it might be better to use a Ruby Version Management tool such as RVM.

 

Regards

Steve.


  • Kelli Shaver and patrickmaciel like this


#1262 Why bundler? Why not bundler?

Posted by james on 14 September 2013 - 07:26 AM

If you are not using bundler you would just use the gem commands passing in arguments for the appropriate action, e.g. "gem install capistrano"

If you have just added 4 gems to your gem file and you need to install them all then it's much simpler just to run the bundle command.

 

It goes a little deeper than that. Bundler handles the gemfile.lock file so you can lock the current gemset, but in reality, bundler is a convenience method, saving you from having to install each gem manually.


  • Rowel and Bharat Soni like this