0% found this document useful (0 votes)
9 views4 pages

Rails Crud Guide

Uploaded by

buro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Rails Crud Guide

Uploaded by

buro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ruby on Rails Manual CRUD Guide

Project Setup

1. Create new Rails app:

rails new blog_app --skip-javascript --skip-hotwire

cd blog_app

Model and Migration

2. Generate Post model:

rails generate model Post title:string content:text

rails db:migrate

Controller

3. Create Posts controller:

rails generate controller Posts

In app/controllers/posts_controller.rb:

class PostsController < ApplicationController

before_action :set_post, only: %i[show edit update destroy]

def index

@posts = Post.all

end

def show; end

def new

@post = Post.new

end

def create

@post = Post.new(post_params)
Ruby on Rails Manual CRUD Guide
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_path, notice: 'Post was successfully deleted.'

end

private

def set_post

@post = Post.find(params[:id])

end

def post_params

params.require(:post).permit(:title, :content)

end

end
Ruby on Rails Manual CRUD Guide
Routes

4. In config/routes.rb:

resources :posts

root "posts#index"

Views

5. Create app/views/posts/ with these files:

index.html.erb:

<h1>All Posts</h1>

<%= link_to 'New Post', new_post_path %>

<ul>

<% @posts.each do |post| %>

<li>

<%= link_to post.title, post %> |

<%= link_to 'Edit', edit_post_path(post) %> |

<%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %>

</li>

<% end %>

</ul>

_form.html.erb (shared):

<%= form_with(model: @post, local: true) do |form| %>

<p>

<%= form.label :title %><br>

<%= form.text_field :title %>

</p>

<p>

<%= form.label :content %><br>

<%= form.text_area :content %>

</p>
Ruby on Rails Manual CRUD Guide
<p>

<%= form.submit %>

</p>

<% end %>

new.html.erb:

<h1>New Post</h1>

<%= render 'form', post: @post %>

<%= link_to 'Back', posts_path %>

edit.html.erb:

<h1>Edit Post</h1>

<%= render 'form', post: @post %>

<%= link_to 'Back', posts_path %>

show.html.erb:

<h1><%= @post.title %></h1>

<p><%= @post.content %></p>

<%= link_to 'Edit', edit_post_path(@post) %> |

<%= link_to 'Back', posts_path %>

Summary

CRUD Actions:

- Create: new, create

- Read: index, show

- Update: edit, update

- Delete: destroy

This guide walks through full manual CRUD setup for a simple Post model in Rails.

You might also like