Inherited Resources Is Powerful Gem for Your Rails Application Controller

Inherited Resources speeds up development by making your controllers inherit all restful default actions so you just have to focus on what is important. In addition to making your controllers follow a pattern, it helps you to write better code by following fat models and skinny controllers convention

HOW TO USE:

First add the gem in your Gemfile:
gem 'inherited_resources'

Now Make a Controller, which you want to create, In my case, I'm creating a POST Controller without Scaffold:

so Just make a file name posts_controller.rb

Now, Open Posts Controller Page and Add code there:
class PostsController < ApplicationController

Now the Tricky Part, You don't need to create any

def index
   Something code.... 
end
def new
   Something code.... 
end
def create
   Something code.... 
end
def edit
   Something code.... 
end
def update
   Something code.... 
end
def destory
   Something code.... 
end

DON'T NEED, Remove this Code and Add Here:

class PostsController < ApplicationController

inherited_resources

def permitted_params
     params.permit(post: [:title, :content])
end


Now Create a MODEL and VIEW as same as you create, That's it.

All the CRUD Action will work auto through Inherited resources Gem.
Note: don't forgot to mention resources :posts in your routes file.


Tags: Ruby on Rails Tutorials, Ruby on Rails Videos, Ruby Gems, Gems Use trick, Ruby code, Rails servers

Comments