/base_controller.rb Secret
Last active
May 7, 2021 00:34
Rails Controller CURD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseController < ApplicationController | |
before_action :set_resource, only: %i[ show edit ] | |
def index | |
resources = resource_class.all | |
instance_variable_set("@#{resources_name}", resources) | |
end | |
def create | |
@resource = resource_class.new(resource_params) | |
respond_to do |format| | |
if @resource.save | |
format.html { redirect_to @resource, notice: "#{resource_name} was successfully created." } | |
format.json { render :show, status: :created, location: @resource } | |
else | |
format.html { render :new, status: :unprocessable_entity } | |
format.json { render json: @resource.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def new | |
resource = resource_class.new | |
instance_variable_set("@#{resource_name}", resource) | |
end | |
def show | |
end | |
def edit | |
end | |
def update | |
@resource = resource_class.find(params[:id]) | |
respond_to do |format| | |
if @resource.update(resource_params) | |
format.html { redirect_to @resource, notice: "#{resource_name} was successfully updated." } | |
format.json { render :show, status: :ok, location: @resource } | |
else | |
format.html { render :edit, status: :unprocessable_entity } | |
format.json { render json: @resource.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def destroy | |
@resource = resource_class.find(params[:id]) | |
@resource.destroy | |
respond_to do |format| | |
format.html { redirect_to "#{controller_name}_url", notice: "#{resource_name} was successfully destroyed." } | |
format.json { head :no_content } | |
end | |
end | |
def set_resource | |
resource ||= resource_class.find(params[:id]) | |
instance_variable_set("@#{resource_name}", resource) | |
end | |
def resource_class | |
@resource_class ||= resource_name.classify.constantize | |
end | |
def resource_name | |
@resource_name ||= controller_name.singularize | |
end | |
def resources_name | |
@resources_name ||= controller_name | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoursesController < BaseController | |
private | |
def resource_params | |
params.require(:course).permit(:title, :credit_hours, :code, :year) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SectionsController < BaseController | |
private | |
def resource_params | |
params.require(:section).permit(:name) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StudentsController < BaseController | |
private | |
def resource_params | |
params.require(:student).permit(:first_name, :last_name, :roll_no, :date_of_birth) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment