I have developed a code for an admin page with crud in ruby on rails. But I have a doubt. Can this code be improved in any way? In therms of design patterns, good practices and etc.
Also, I did separate the crud actions from the AdminController Class. Is this considered a good practice at all? Or is there any way I can improve it even further? Thank you.
# app/controllers/game_controller.rb
class GameController < ApplicationController
def create
Game.create(game_params)
end
def update
game = Game.find(params[:id])
if (game.update_attributes(game_params))
return true
end
return false
end
def read
game = Game.find(params[:id])
end
def delete
if (Game.find(params[:id]).destroy)
return true
else
return false
end
end
private
def game_params
params.require(:game).permit(:description,:name,:category,:status,:boxshot)
end
end
# app/controllers/admin_controller.rb
class AdminController < ApplicationController
before_action :require_logged_in_user
before_action :define_action
def index
@games = Game.all
@admin = get_admin_details()
end
def read
render :json => @action.read
end
def update
if ([email protected])
render :text => "false"
end
render :text => "true"
end
def delete
if([email protected])
render :text => "false"
end
render :text => "true"
end
def logout
reset_session
redirect_to(:controller => 'login', :action => 'view')
end
private
def define_action
@action = GameController.new()
@action.params = params
I18n.locale = cookies['language']
end
def get_admin_details
admin = User.find(require_logged_in_user())
end
def require_logged_in_user
if(session[:user_id])
return session[:user_id]
else
redirect_to(:controller => 'login', :action => 'view')
end
end
end