This project made based on Rails Tutorial | Building a Blog with Ruby on Rails 6 using Rails 6.1.6 and Ruby 2.6.3p62. I am curious whether Rails can be used to make a blog like Wordpress so then I tried to follow this tutorial.
Generate New Project
Gem Configuration
Generate new project by running rails new simple-blog
. Go to project folder cd simple-blog
and open up Gemfile then commented out gem ‘jbuilder’ since we don’t want to generate JSON or API like project. And add gem devise for authentication then run bundle
.
|
|
Run rails g controller Home index
in the terminal. This will create Home Controller with index action. It will be deleted later but for now we just want to have home index. So if we run local server rails s
and go to localhost:3000 page on the web browser, we will see home page index.
Setting Authentication with devise
Lets create devise modal rails g devise:install
and this is gonna take us to few things that we need to care. Follow the step and run rails g devise:views
in the terminal.
Then let’s create modal called Author with login authentication, rails g devise Author
, then migrate it, rails db:migrate
. Then try to open localhost:3000/authors/sign_up on the browser and we’ll see a sign-up page. Try to sign-up your first account and, TA-DA, you’ve got authentication set up in just a couple of minutes.
Create Post and Author Authentication
Generate post modal by running this line in the terminal. This command will create field title - since we don’t specify the data type, it will generate string type -, published, published_at and author.
|
|
Then try run localhost:3000/posts on your browser and play around with it and see how it all works.
Create Authentication for Post
We want to post only can be accessed when Author get authentication so we move posts resources under Author scope in route
|
|
Then try to see the result localhost:3000/posts and you’ll get a ‘Routing Error - uninitialized constant Authors’ because, instead of PostsController, it’s wanting to look at Authors::PostsController. To solve this, create new folder called authors under app/controllers and move post_controller.rb in it. Also in that file, wrap up all existing code inside module Authors / end.
And now go back to web browser and refresh. Oh no, You still see an error ‘No template for interactive request’. To solve this, you just need to crate new folder authors under app/views and move folder posts into the authors folder.
Check again the page and now it should work.
Create one-many relation for Author and Post
Add relationship between posts and author ‘has_many :posts’ in the file app/models/author.rb so the code looks like
|
|
Enable Authentication
Create new controller called authors_controller.rb under app/controllers folder, put this line in the file,
|
|
save it and change inherit code from class PostsController < ApplicationController to class PostsController < AuthorsController in the posts_controller.rb file.
Let’s try run localhost:3000/posts and perhaps there is no differences since you’ve just sign up new account. Open another browser and access the address, then you see the new browser redirect to login page.
Author Layout
Specify layout for author in the authors_controller.rb file
|
|
Refresh page localhost:3000/posts in the browser then you get error ‘Tempate is missing’. Solving this error, you need to create new template file called authors.html.erb in the folder app/views/layouts and copy all codes in application.html.erb to the new file.
Setting Post Layout
Setting bootstrap
Open BootstrapCDN on web browser and copy bootstrap.min.css into authors.html.erb file and paste under head tag. Next, Go to app/stylesheets and delete scaffold.scss file.
Setting Post Controller
In the file app/views/authors/posts/_form.html.erb, take away publish and author related fields.
Edit posts_controller.rb. Change POST to ‘current_author.posts’ in ‘set_index’, ‘create’, ‘index’ and ’new’.
Delete unwanted fields that we want to send from form so the final codes look like
Setting Author Layout
Edit parent layout app/layouts/authors.html.erb so that ‘<%= yield %>’, code that place chlid template, wrap in class container
|
|
Setting Posts Layout
Edit child page layout for index action ‘app/views/authors/posts/index.html.erb’
|
|
In here, we also change link to access post-edit layout instead of showing posts, then remove template to show them. Delete the file ‘app/views/authors/posts/show.html.erb’. The next, ‘show’ action def and ‘show’ symbol in before_action in the file ‘posts_controller.rb’ shall be removed.
|
|
menit 26:53
. . . . .
Create Element
|
|
run db:migrate
and move file elements_controller.rb from controllers folder into controllers/authors folder. Wrap all existing codes into module Authors and end tags. Then change enherit tag class ElementsController < ApplicationController to class ElementsController < AuthorsController.
In the route.rb we want to make element nested in the post resource so the code will look like
|
|
Check the routes for element by running rails routes | grep element
in the terminal.