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
<h1>Categories</h1>

<ul>
<% @categories.each do |category| %>
<li><%= link_to category.name, category %></li>
<% end %>
</ul>
“`

10. Open the `app/views/categories/show.html.erb` file and update it with the following code:
“`erb
<h1><%= @category.name %></h1>

<h2>Topics</h2>

<ul>
<% @category.topics.each do |topic| %>
<li><%= link_to topic.title, category_topic_path(@category, topic) %></li>
<% end %>
</ul>
“`

11. Open the `app/views/topics/show.html.erb` file and update it with the following code:
“`erb
<h1><%= @topic.title %></h1>

<h2>Posts</h2>

<ul>
<% @topic.posts.each do |post| %>
<li><%= post.content %></li>
<% end %>
</ul>

<%= 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.