2017-04-14 7 views
0

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 
+1

最初の一見したところで、最初に出てきたのは、インデントと空白が混乱し、そこのRubyスタイルのすべてのガイドラインに違反していることです。 –

+0

ええ、私は実際にここにそれを貼り付けるときにうんざりしました。私はそれをできるだけ編集します。 –

+0

改善/修正できる点がいくつかあります。このガイド(https://github.com/bbsosov/rails-style-guide)を最初に読んで、コードを更新してからフィードバックを返すことをおすすめします。 – Gerry

答えて

3

正直言って、かなり改善することができます。ここで私はもう少し徹底的レールの基礎を学ぶためにいくつかの時間がかかるだろうGameController

class GameController < ApplicationController 
    before_action :find_game, only: [:update, :show, :delete] 

    def new 
    @game = Game.new 
    end 

    def create 
    @game = Game.new(game_params) 

    if @game.save 
     redirect_to game_path(@game) 
    else 
     render :new 
    end 
    end 

    def edit 
    end 

    def update  
    if @game.update(game_params) 
     redirect_to game_path(@game) 
    else 
     render :edit 
    end 
    end 

    def show 
    end 

    def delete 
    if @game.destroy 
     redirect_to games_path 
    else 
     render :show 
    end 
    end 

    private 

    def find_game 
    @game = Game.find(params[:id]) 
    end 

    def game_params 
    params.require(:game).permit(:description, :name, :category, :status, :boxshot) 
    end 
end 

に私のテイクがあります。 Railscastsをチェックしてください。それは古いですが、まだ良い情報です。プレミアムエピソードはYoutubeで見ることができます。

あなたが提供するアドバイスを使用するスタイルガイドを見ることもできます。

何をしても、最も重要なことは一貫性を保つことです。同じインデント量、同じクラス構造、同じ代入と条件構造などを維持します。

2つ目の質問に答えるには、新しいCRUDアクションごとに新しいコントローラが必要です。 1トンの機能で1つのコントローラに試してみてください。それを簡単に保つ、ラインは安いです。

+0

最後の段落に対応するRailsマントラは、「Fat Model、Skinny Controller」ですが、現在のトレンドは「Skinny Controller 、シンプルモデル、多くのサービス " –