Creating a Forum with Ruby on Rails

Creating a Forum with Ruby on Rails

To create a forum using Ruby on Rails, you can follow these steps:

1. Set up a new Rails application by running the following command in your terminal:
“`
rails new forum
“`

2. Change into the newly created application directory:
“`
cd forum
“`

3. Generate the necessary models, controllers, and views for the forum:
“`
rails generate scaffold Category name:string
rails generate scaffold Topic title:string category:references
rails generate scaffold Post content:text topic:references
“`

4. Run the database migrations to create the necessary tables:
“`
rails db:migrate
“`

5. Open the `config/routes.rb` file and update it with the following routes:
“`ruby
Rails.application.routes.draw do
resources :categories do
resources :topics do
resources :posts
end
end
root ‘categories#index’
end
“`

6. Open the `app/controllers/categories_controller.rb` file and update it with the following code:
“`ruby
class CategoriesController < ApplicationController
def index
@categories = Category.all
end

def show
@category = Category.find(params[:id])
end
end
“`

7. Open the `app/controllers/topics_controller.rb` file and update it with the following code:
“`ruby
class TopicsController < ApplicationController
before_action :set_category

def index
@topics = @category.topics
end

def show
@topic = @category.topics.find(params[:id])
end

def new
@topic = @category.topics.new
end

def create
@topic = @category.topics.new(topic_params)
if @topic.save
redirect_to category_topic_path(@category, @topic), notice: ‘Topic was successfully created.’
else
render :new
end
end

private

def set_category
@category = Category.find(params[:category_id])
end

def topic_params
params.require(:topic).permit(:title)
end
end
“`

8. Open the `app/controllers/posts_controller.rb` file and update it with the following code:
“`ruby
class PostsController < ApplicationController
before_action :set_category_and_topic

def create
@post = @topic.posts.new(post_params)
if @post.save
redirect_to category_topic_path(@category, @topic), notice: ‘Post was successfully created.’
else
render ‘topics/show’
end
end

private

def set_category_and_topic
@category = Category.find(params[:category_id])
@topic = @category.topics.find(params[:topic_id])
end

def post_params
params.require(:post).permit(:content)
end
end
“`

9. Open the `app/views/categories/index.html.erb` file and update it with the following code:
“`erb

Categories


    <% @categories.each do |category| %>
  • <%= link_to category.name, category %>

  • <% end %>

“`

10. Open the `app/views/categories/show.html.erb` file and update it with the following code:
“`erb

<%= @category.name %>

Topics


    <% @category.topics.each do |topic| %>
  • <%= link_to topic.title, category_topic_path(@category, topic) %>

  • <% end %>

“`

11. Open the `app/views/topics/show.html.erb` file and update it with the following code:
“`erb

<%= @topic.title %>

Posts


    <% @topic.posts.each do |post| %>
  • <%= post.content %>

  • <% end %>

<%= form_with(model: [@category, @topic, @topic.posts.build], local: true) do |form| %>
<%= form.text_area :content %>
<%= form.submit ‘Create Post’ %>
<% end %>
“`

12. Start the Rails server by running the following command:
“`
rails server
“`

You should now be able to access your forum at `http://localhost:3000`. You can create categories, topics, and posts through the provided forms and navigate through the forum structure.

Let's talk

If you want to get a free consultation without any obligations, fill in the form below and we'll get in touch with you.