0

私は通常のブラウザベースのRailsゲームアプリケーションを開発しました。現在、CloudMailinをミックスに追加して、電子メールを介して代替インターフェースを効果的に公開しています。

代表的な例として、私の既存のcreate行動を考えてみましょう。

class GamesController < ApplicationController

  def create
    @game = Game.params[:new]

    if @game.random_create
      # Asked to create a game using random choices.
      # Make the random choices, then present it to the user for tweaking
      @game.expand_random_choices

      render :action => new
    else
      # Fully specified. Create the game
      begin
        @game.save!

        # ...other work including DB operations ...

        flash[:notice] += 'Game was successfully created.'
        redirect_to :action => :play, :id => @game
      rescue ActiveRecord::RecordInvalid
        @game.valid?
        render :action => 'new'
      end
    end
  end
end

これで、Cloudmailinメールを処理するためのPbemControllerができました。

class PbemController < ApplicationController

  # Handle inbound email
  def handle
    if email_is_a_game_creation
    ...
    end

    render :text => "Handled"
  end
end

createから既存の動作を呼び出すための最良かつ最も乾燥した方法は何PbemControllerですか?私の唯一の本当の選択肢は、各「共有」アクションをモジュールに/lib' and抽出し、それを各コントローラーに含めることですか?

4

1 に答える 1

1

通常、最良のオプションは、モデルにできるだけ多く移動することです。そうすれば、コントローラーから実行できるコードはすべて、ハンドラーからも実行できます。

あなたはそのような方法create_or_build_randomをここで潜在的に助けることができるかもしれませんか?

于 2011-08-10T13:56:53.310 に答える