To implement a basic blog application in Ruby on Rails, we'll need to plan out the essential components: models, controllers, and routes. Let's start by defining the scope of our blog application. We'll keep it simple with two main models: User and Post. A user can create, edit, and delete their own posts, and view any post.
rails generate model User name:string email:string password_digest:string
We'll use has_secure_password for password management.
Post
ruby
rails generate model Post title:string content:text published_at:datetime user:references
2. Define Relationships
In app/models/user.rb:
ruby
class User < ApplicationRecord
has_secure_password
has_many :posts
end
In app/models/post.rb:
ruby
class Post < ApplicationRecord
belongs_to :user
end
3. Generate Controllers
UsersController
sh
rails generate controller Users index show new create edit update destroy
PostsController
sh
rails generate controller Posts index show new create edit update destroy
4. Define Routes
In config/routes.rb:
ruby
Rails.application.routes.draw do
resources :users, only: [:index, :show, :new, :create, :edit, :update, :destroy]
resources :posts, only: [:index, :show, :new, :create, :edit, :update, :destroy]
# other routes
end
5. Implement Controller Actions
UsersController
Implement actions for handling user CRUD operations.
PostsController
Implement actions for handling post CRUD operations. Ensure that editing and deleting posts is restricted to the post's author or an admin (if the concept of admin users is introduced).
6. Views
Create views for each controller action as needed. For a RESTful API, you might not need HTML views if you're serving JSON responses. If that's the case, ensure your controllers render JSON instead.
7. Testing
Write tests for your models and controllers to ensure they work as expected. Rails provides minitest by default, but you can use other testing frameworks like RSpec.
Example Controller Implementation
Here's an example of what the PostsController might look like:
ruby
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@posts = Post.all
end
def show
end
def new
@post = Post.new
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to @post, notice: 'Post was successfully created.'
else
render :new
end
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post, notice: 'Post was successfully updated.'
else
render :edit
end
end
def destroy
@post.destroy
redirect_to posts_url, notice: 'Post was successfully destroyed.'
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content, :published_at)
end
end
This is a basic outline. Depending on your application's needs, you might add authentication (e.g., with Devise), authorization (e.g., with Pundit), or other features. Remember to adjust the implementation based on your specific requirements.