Getting started with Ruby on Rails: A blog app (Part 2)
Welcome to art 2 of our ruby on rails app.
Disclaimer
Personally when developing a web application, i love going for the bones before going for the meat… i would first get an app functioning but with horrible layouts, then later come back and finish with the front-end… kinda weird, right?. In this tutorial series am going to take the same approach. Take heart, ruby has some default layouts so it wont hurt that much.
1. Add authentication with devise
Add devise gem to your gemfile
gem 'devise'
Run bundle install to install devise into your app, and complete the installation using the following command. you can learn more about the devise gem here.
rails generate devise:install
Now lets add users into our app, as for me I will call them authors
rails generate devise author
And well now migrate our database
rails db:migrate
In our app, we only want authors(users) who have logged into our app to post articles, so we need to add some restrictions. at the very top of our app/controllers/articles_controller.rb, add this line.
before_action :authenticate_author!, except: [:show, :index]
Its important for it to be at the top of the file as rails follows top to bottom order when it comes to executing controller restrictions. here we are restricting every other action except showing list of articles or reading an article.
2. Add associations
Here we going to link our authors with articles. typically, authors can post multiple articles, but an article can only have one author. so we have a one-to-many relationship in our case.
In app/models/Article.rb
class Article < ApplicationRecord belongs_to :authorend
In app/models/Author.rb
class Author < ApplicationRecord has_many :articlesend
Create a new migration from your terminal to add author_id to the article.
Here will will add author id, which will be an integer
rails g migration add_author_id_to_articles
Now in root folder/db/migrate, you will see the migration file. mine looks like this
class AddAuthorIdToArticles < ActiveRecord::Migration[6.0]def changeadd_column :articles, :author_id, :integerendend
Don’t forget to run the database migrations with rake db:migrate
Now in our articles controller, edit the create action, and add this line.
# POST /articles.jsondef create@article = Article.new(article_params)@article.author = current_author #add this linerespond_to do |format|if @article.saveformat.html { redirect_to @article, notice: 'Article was successfully created.' }format.json { render :show, status: :created, location: @article }elseformat.html { render :new }format.json { render json: @article.errors, status: :unprocessable_entity }endendend
Here we are associating a new article with the current logged in user. with this, you can call author.articles, and it will return a list of all articles by the user.
I won’t go into design, but i have attached my final app screenshots. you can also find this code on GitHub.
Merry Christmas and happy new year!