Ruby on Rails: Methods for Implementing Nested Resources
Discover in our tutorial how to create nested resources for your application using Ruby on Rails. We guide you step-by-step through the process – from model creation to controller customization. Optimize your Rails application today for more efficient data management and an improved user experience!
Ruby on Rails is a web framework written in Ruby that provides developers with a specific approach to application development. Working with Rails offers developers:
- Conventions for handling things like routing, stateful data, and asset management.
- A solid foundation in the model-view-controller (MVC) architecture pattern, which separates application logic contained in models from the presentation and routing of application information.
As you increase the complexity of your Rails applications, you will likely work with multiple models that represent the business logic of your application and interact with your database. Adding related models means creating meaningful relationships between them, which then affects how information is processed through your application’s controllers and how it is captured and returned to users through views.
Prerequisites – Ruby on Rails
To follow this tutorial, you’ll need:
- A local computer or development server with Ubuntu 18.04. Your development machine should have a non-root user with administrator privileges and a firewall configured with ufw.
- Node.js and npm installed on your local computer or development server. This tutorial uses Node.js version 10.16.3 and npm version 6.9.0.
- Ruby, rbenv, and Rails installed on your local computer or development server.
- SQLite installed and a basic application created for shark information.
Step 1: Creating a Nested Model
Our application will use Active Record relationships to establish a connection between Shark and Post models: Posts will belong to specific Sharks, and each Shark can have multiple Posts. Our Shark and Post models will therefore be connected by belongs_to and has_many relationships.
The first step to building the application this way is to create a Post model and associated resources. For this, we can use the `rails generate scaffold` command, which provides us with a model, a database migration to modify the database schema, a controller, a complete set of views for managing standard CRUD operations, and templates for parts, helpers, and tests.
rails generate scaffold Post body:text shark:references
Once you’ve run the command, you’ll see output confirming the resources Rails has generated for the application. Before proceeding, you can check your database migration file to verify the relationship now established between your models and database tables.
class CreatePosts < ActiveRecord::Migration[5.2]
def change
create_table :posts do |t|
t.text :body
t.references :shark, foreign_key: true
t.timestamps
end
end
end
Step 2: Specifying Nested Routes and Relationships for the Parent Model
Rails has already set up the belongs_to relationship in our Post model, thanks to the :references keyword in the `rails generate scaffold` command. But to make this relationship work properly, we also need to specify a has_many relationship in our Shark model. Additionally, we’ll make adjustments to the default routing Rails has provided to make Post resources children of Shark resources.
class Shark < ApplicationRecord
has_many :posts , dependent: :destroy
validates :name, presence: true, uniqueness: true
validates :facts, presence: true
end
Rails.application.routes.draw do
resources :sharks do
resources :posts
end
root ‘sharks#index’
end
Step 3: Updating the Post Controller
The association between our models provides us with methods that we can use to create new Post instances linked to specific Sharks. To use these methods, we need to add them to our Post controller.
class PostsController < ApplicationController
before_action :get_shark
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
@posts = @shark.posts
end
def new
@post = @shark.posts.build
end
def create
@post = @shark.posts.build(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to shark_posts_path(@shark), notice: ‘Post was successfully created.’ }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to shark_post_path(@shark), notice: ‘Post was successfully updated.’ }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to shark_posts_path(@shark), notice: ‘Post was successfully destroyed.’ }
format.json { head :no_content }
end
end
private
def get_shark
@shark = Shark.find(params[:shark_id])
end
def set_post
@post = @shark.posts.find(params[:id])
end
def post_params
params.require(:post).permit(:body)
end
end
Step 4: Adjusting the Views
In this step, we’ll modify our view templates to better display Posts and their associated Sharks. We’ll start by revising the form for our Posts, and then we’ll adjust the index view to display Posts in connection with specific Sharks.
Form for Posts
Our `_form.html.erb` form, reused across multiple Post templates, requires a few changes. Instead of passing only the Post model to the `form_with` helper, we’ll pass both the Shark and Post models and set Post as a nested resource.
<%= form_with(model: [@shark, post], local: true) do |form| %>
# Additional changes to the form
<% end %>
Index View for Posts
The index view displays Posts in connection with a specific Shark. We’ll make some updates to improve display and navigation.
<% @posts.each do |post| %>
<%= post.body %><%= post.shark.name %><%= link_to ‘Show Shark’, [@shark] %><%= link_to ‘Edit Post’, edit_shark_post_path(@shark, post) %><%= link_to ‘Destroy Post’, [@shark, post], method: :delete, data: { confirm: ‘Are you sure?’ } %>
<% end %>
Additional Adjustments
We’ll also update other templates to account for changes in our form. This includes the `new.html.erb` and `edit.html.erb` templates for Posts and the `show.html.erb` template for Sharks.
New and Edit Posts
References in the templates for new and edit Posts will be updated accordingly.
Displaying Sharks
The `show.html.erb` template for Sharks will be extended to include a section for Posts and a link to add new Posts.
Step 5: Adding Validations and Testing the Application
Finally, we’ll add validations to the Post model to ensure data consistency. Once validations are added, we can test the application to ensure everything works smoothly.
With these final changes, you’re ready to run your migrations and test the application.
Ruby on Rails – Conclusion
With the adjustments made, you have enhanced your Rails application and prepared it for future development. If you want to learn more about routing and nested resources, the Rails documentation offers an excellent source of information. Effective Methods for Implementing Nested Resources in Ruby on Rails