Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts

Wednesday, 5 December 2012

Notes on using Heroku

These are a few common things I find myself looking up when working with Heroku - I update this post if I find myself repeated searching for how to do the same tasks.

Running commands from a folder not linked to Heroku

Simply append --app APP_NAME to any relevant Heroku command:
HEROKU_COMMAND --app APP_NAME  

Deployment

If trying to push to Heroku:
 git push heroku master  
but get a publickey error:
 Permission denied (publickey).  
 fatal: The remote end hung up unexpectedly  
You need to add your public key to Heroku.
 heroku keys:add path_to_publickey  
If you don't have a public key, Heroku can prompt and generate one for you - simply use the command:
 heroku keys:add  

Resetting the application database

 heroku pg:reset DATABASE_URL  
To reset the database without a validation prompt, use:
 heroku pg:reset DATABASE_URL --confirm APP_NAME  

Rake

To run a rake task on Heroku, create the task in app/lib/tasks (Rails), test locally then deploy. To run, use:
 heroku run rake TASK_NAME


If you have any useful commands you regularly use while working with Heroku, let me know in the comments below!

Wednesday, 18 July 2012

Betty and George - an overview


This is the first in a series of posts detailing my latest hobby project. The bulk of the work was completed in under 10 hours, with minor styling tweaks taking a little longer to get right. The project is currently hosted here, and the source code is on Github here. As usual, please post any comments or opinions in the comments below, or tweet me directly @matalangilbert.

Introduction

Betty and George are Artificial Intelligence entities - they chat with each other in real time, and, somewhat incredibly in my opinion, make sense most of the time! They're sometimes witty, sometimes poignant, and sometimes inane, but they keep talking, no matter what!


Technical Overview

The application is split into two main parts. The backend is a Ruby program managing two Cleverbot instances (http://www.cleverbot.com), juggling responses between each instance. There's not too much to this, but I will go over the technical details in a future post.

Each conversational response is posted to a Rails application endpoint by the Ruby program, and then the front-end takes over. The frontend is backed by a Rails application, which receives data from the Ruby program, and displays it on the live site. There's a couple of neat techniques used on the main application page, which update the page in the background. The first is a background AJAX request, which hits the server every 5 seconds, retrieving new responses and adding them to the top of the scrolling conversation on the site. The second is an "Endless River of News" style endless scroll implementation. This invisibly loads more of the conversation as the user scrolls down the page, in much the same way as Twitter and other sites do. Individually, neither of these techniques are particularly new on their own, but combining them seamlessly was technically satisfying. I'll talk more about these techniques in a dedicated future post.


Hosting

At present, both the conversation program and the Rails application are hosted at Heroku. A future post will detail a few of the technical subtleties I discovered while deploying the live Betty and George website.

Now visit Betty and George!

OK, so this was just a brief overview of the Betty and George architecture - now go check them out, and let me know what you think in the comments below!


Wednesday, 25 April 2012

attr_accessor and attr_accessible in Rails

While trying to figure out why I was getting this error in a Rails application:
 WARNING: Can't mass-assign protected attributes  

I realised I'd confused the two below terms in my Rails model:

 attr_accessor  
is built into Ruby and defines a getter and setter method for a class attribute.


 attr_accessible  
is defined by Rails and makes the attribute mass-assignable.

Generally, in a Rails application, it's attr_accessible we want to be using.

Friday, 20 April 2012

Undoing a Rails generate

To undo an action in Rails (generation of a scaffold, model, migration, etc), you can use
 rails destroy  

For example, generating and then destroying a controller called Posts would look like this:

 $ rails generate controller Posts  
    create app/controllers/posts_controller.rb  
    invoke erb  
    create  app/views/posts  
    invoke test_unit  
    create  test/functional/posts_controller_test.rb  
    invoke helper  
    create  app/helpers/posts_helper.rb  
    invoke  test_unit  
    create   test/unit/helpers/posts_helper_test.rb  
    invoke assets  
    invoke  coffee  
    create   app/assets/javascripts/posts.js.coffee  
    invoke  scss  
    create   app/assets/stylesheets/posts.css.scss  

$ rails destroy controller Posts  
    remove app/controllers/posts_controller.rb  
    invoke erb  
    remove  app/views/posts  
    invoke test_unit  
    remove  test/functional/posts_controller_test.rb  
    invoke helper  
    remove  app/helpers/posts_helper.rb  
    invoke  test_unit  
    remove   test/unit/helpers/posts_helper_test.rb  
    invoke assets  
    invoke  coffee  
    remove   app/assets/javascripts/posts.js.coffee  
    invoke  scss  
    remove   app/assets/stylesheets/posts.css.scss  

Wednesday, 4 April 2012

Error installing pg gem in Linux

If installing the pg gem in Linux fails with this error:

checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...
 An error occured while installing pg (0.13.2), and Bundler cannot continue.


Installing libpq-dev should solve the problem.
 sudo apt-get install libpq-dev  

Friday, 23 March 2012

Installing Rails 3.2 into Linux Mint (and other Linux distributions)


1) Install RVM:
  bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)   


2) Install or update RubyGems:
 gem update --system  


3) Install Rails:
 gem install rails  


That's it! Any problems, let me know below.

RVM Installation Problems in Linux Mint

I was having problems installing RVM into Linux Mint, it seems that somehow RVM is/was broken for Ubuntu distros. I was having issues with RVM saying that it couldn't fetch the ruby source, and curl errors as well. Purging and reinstalling RVM:

Remove RVM:
 sudo apt-get --purge remove ruby-rvm  
 sudo rm -rf /usr/share/ruby-rvm /etc/rvmrc /etc/profile.d/rvm.sh  

Close terminal window and open a new one then reinstall RVM with:
 bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)  

as per this StackOverflow answer solved my problems.