ruby on railで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/bbsosov/rails-style-guide)を最初に読んで、コードを更新してからフィードバックを返すことをおすすめします。 – Gerry