我已经开发了一个管理页面的代码,在轨道上的红宝石crud。但我有疑问。这个代码可以以任何方式改进吗?在设计模式,良好做法和等Ruby on Rails的设计模式和良好做法
而且千卡,我也分开了AdminController类CRUD操作。这被认为是一种很好的做法吗?或者有什么方法可以进一步改进?谢谢。
# 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
快速第一眼后,跳转右出的第一件事是,缩进和空白都是一团糟并违反几乎每一个Ruby风格指南那里。 –
是的,当我把它粘贴在这里时,我真的搞砸了。我会尽快编辑它。 –
有几件事情可以改进/修复。我建议先通过本指南(https://github.com/bbatsov/rails-style-guide),更新您的代码,然后再回来获取更多反馈。 – Gerry