8. Page Links

September 2015 · 3 minute read

Now that I have my layouts set up and some working partials. I can follow my previous partials instruction set to render a form which will update my income information. Of course I’ll need a few modifications in my income controller as well.

First, I’m going to create a new.html.erb file under app/views/income which will later render a partial as in my previous tutorial.

In the routes file I can include the following line to test out my new page and navigate to localhost300/income/index

get ‘income/index’

Now in the income_controller.rb I can declare set_income and some income_params which tell the compiler which table to access in the database and what information is available.


  def set_income

  def income_params
    params.require(:income).permit(:wages, :other_income, :income_total)


I can also specify which actions can access the information parsed by set_income. For example, def new is going to link up to a new page which will allow me to enter data and store it in the table before taking me back to my home page.

I can now create another page called new.html.erb in the app/views/income folder. A simple link will navigate to this page from the app/views/income/index.html.erb

After creating the new file, we first need to either exit the rails server and run rake routes or open a new tab in the terminal and run rake routes. This will show us a list of all available routes in the system. If the one we are looking for isn’t there, we can always create it later.

It is clear to see that there are significantly more routes available than what are specified in the routes.rb, this is because every resource automatically comes with seven routes - index, create, new, edit, show, update and destroy. You can also see that I have some double ups.

Rails.application.routes.draw do
  get 'welcome/index'
  get 'income/index'
  resources :income

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

I have two different routes, both of which will get me income#index and two more for welcome#index, now in this case I do not actually have a use for two different routes to either of these paths. I can simply remove the get ‘welcome/index’ and get ‘income/index’ in the routes file.

Rails.application.routes.draw do
  resources :income

  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

This seems like only a minor optimisation but in the long run the compiler reads every single line of code, so there’s no point in wasting resources and time later on.

Back to the new link. We can see from the list of routes available that the path we need to take is a ‘get’ action

new_income GET /income/new(.:format) income#new<

By running a link_to path in the app/views/income/index.html.erb we can wire up the ‘new’ page.

  <%= link_to 'New', new_income_path %>

After saving and reloading the income/index page we can see there is a now a link titled “new”

Simply clicking on the link will test it out. It redirects the new page we created earlier. You can see in the web browsers path that the route is income#index (a resource default option).