Railscast 035 Custom REST Actions

The GitHub Repo

The Heroku App

This is one of those episodes I needed to see. I haven’t had to use the collection or member blocks in the routes file before, but I’m glad to know what they mean now. member and collection allows the developer to create custom RESTful routes that extends the rails conventions. A member route appends after the /:model/:id, so the controller action has the params[:id]. A collection route appends after the /:model route.

Examples of these two were the routes created in this episode.

resources :tasks do
  get 'completed', on: :collection
  put 'complete', on: :member

This creates the normal RESTful Rails routes, but it also create two custom routes. /tasks/completed/ and /tasks/:id/complete. The Rails Guides for Routing was useful for further reading.


def completed
  @tasks = Task.where(completed: true)

def complete
  @task.update_attribute :completed, true
  flash[:notice] = 'Task Completed'
  redirect_to completed_tasks_path

The two custom actions were completed and complete The completed action returns a tasks instance variable where completed is true. The complete action updates an attribute to true, then redirects to the completed tasks view with a flash notice.

To complete a task, the episode shows creating a link with the put method being sent the complete_task_path while sending the task’s id.

Heroku Issue

For some reason when I tried to click on this ‘complete task’ link, heroku is giving me a ‘page doesn’t exist’ error. Locally the link works however. I have opened a StackOverflow question about the issue. I don’t think you need a view for that action because it redirects to the completed_tasks_path. Even the rake routes show a PUT for /tasks/:id/complete. I did discover that the Rails core team has been switching over to the Patch verb over the put http verb. This adheres to RFC specification for partial updates. Even though I tried to change out put for patch, I was unable to get this link working.

Heroku Could not find * in any of the sources

When I was uploading 023-counter-cache-column, I ran into the following error

-----> Ruby app detected
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-2.0.0
-----> Installing dependencies using 1.5.2
       New app detected loading default bundler cache
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
       Fetching gem metadata from https://rubygems.org/..........
       Fetching additional metadata from https://rubygems.org/..
       Could not find sprockets-2.11.1 in any of the sources
       Bundler Output: Fetching gem metadata from https://rubygems.org/..........
       Fetching additional metadata from https://rubygems.org/..
       Could not find sprockets-2.11.1 in any of the sources
 !     Failed to install gems via Bundler.

 !     Push rejected, failed to compile Ruby app

The issue was with my Gemfile.lock. I needed to run bundle update to make sure this file was updated. After I ran bundle update sprockets changed to version 2.11.0. This makes the error understandable because 2.11.1 would be unavailable through rubygems.org unless specified.

Referenced StackOverflow

Heroku app and column data type

I ran into an error when I was pushing the last eager-loading app onto Heroku. I had the following error

2014-03-13T07:45:24.207959+00:00 app[web.1]: : SELECT products.*, categories.name as category_name FROM "products" INNER JOIN "categories" ON "categories"."id" = "products"."category_id"  ORDER BY categories.name):
2014-03-13T07:45:24.207959+00:00 app[web.1]:     13:     <% @products.each do |product| %>
2014-03-13T07:45:24.207959+00:00 app[web.1]:     14:       <tr>
2014-03-13T07:45:24.208979+00:00 app[web.1]:     15:         <td><%= link_to product.name, product %></td>
2014-03-13T07:45:24.208979+00:00 app[web.1]:     16:         <td><%= number_to_currency product.price %></td>
2014-03-13T07:45:24.208979+00:00 app[web.1]:   app/views/products/index.html.erb:13:in `_app_views_products_index_html_erb___1529240047445058566_70114821600640'
2014-03-13T07:45:24.208979+00:00 app[web.1]: 
2014-03-13T07:45:24.208979+00:00 app[web.1]: 
2014-03-13T07:45:24.208979+00:00 app[web.1]: I, [2014-03-13T07:45:24.200411 #2]  INFO -- : Started GET "/" for at 2014-03-13 07:45:24 +0000
2014-03-13T07:45:24.208979+00:00 app[web.1]: I, [2014-03-13T07:45:24.201343 #2]  INFO -- : Processing by ProductsController#index as HTML
2014-03-13T07:45:24.208979+00:00 app[web.1]: E, [2014-03-13T07:45:24.204799 #2] ERROR -- : PG::UndefinedFunction: ERROR:  operator does not exist: integer = character varying

After some searching around I found that my category_id on my products table was set to the datatype of string. My first attempt to fix this was to change the datatype of the category_id directly.

 class ChangeCategoryIdInProducts < ActiveRecord::Migration
   def self.up
     change_column :products, :category_id, :integer

   def self.down
     change_column :products, :category_id, :string

but this gave the error of

 PG::DatatypeMismatch: ERROR:  column "category_id" cannot be cast automatically to type integer

So directly changing the column does work, so I went with a stronger method. I removed the column category_id that was a string, and I added another column category_id that was an integer.

class ChangeCategoryIdForProducts < ActiveRecord::Migration
  def self.up
    remove_column :products, :category_id
    add_column :products, :category_id, :integer

  def self.down
    remove_column :products, :category_id
    add_column :products, :category_id, :string

This solved the problem I was having, but when I wanted to run rake db:reset Heroku gave me

FATAL:  permission denied for database "postgres"
DETAIL:  User does not have CONNECT privilege.

This is because you cannot drop a PG database on Heroku. The documentation mentions that you could run heroku pg:reset, but I was unable to get that working. I instead manually deleted the herokuapp, then re-uploaded the app onto heroku. When doing this, you have to remember to also remove the git remote the heroku toolbelt generated.

Make all Railscast Projects

I came across a post about someone asking what was the best way to learn Ruby on Rails via Quora. One answer suggested one of the best ways was to follow along with every Railscast project then upload the repo to both GitHub and Heroku. I liked the concept, so I have bought a membership of Railscast and have downloaded all the episodes.

While the goal is to create every project, I will watch every episode and decide if it would be useful to make the project or not. Some episodes are just small tips, so creating a new project just to use that one tip seems inefficient.

I might be repeating what episodes showed, but I found that writing about what I learnt from the episode solidifies what I have learnt. Similar to language learning, you need a balance of input and output to get good at any skill. The input here are the Railscast episodes and the things I look up to finish the apps, and the output are the working app and the blog post.

The first few days of trying this method, I created an app then wrote up the blog post. This takes a lot of context switching between tasks. I am currently trying to go through as many Railscasts in one day, then writing about each the following day.