2

私は基本的に、ロジックのために別のjsファイルの内容を返す必要があるアクションを持っています。どうすればこれを行うことができますか?ありがとう

app/controllers/classrooms_controller.rb

def create
  if params[:test_logic]
    respond_to do |format|
      format.js { render 'create_differently' } # This doesn't work.
    end
  else
    redirect_to root_path
  end
end

app/views/classrooms/create_differently.js.erb

alert('hi')
4

1 に答える 1

6

追加する必要があります

 :layout => false 

js ファイルの html レイアウトのレンダリングを回避します。

さらに、このように別の js ファイルを定義することもできます

:template => "classrooms/create_differently.js.erb"

両方一緒に:

 format.js { 
    render :template => "classrooms/create_differently.js.erb", 
           :layout => false  
 }

ブラウザ ベースのテストでは、html ではなく js を呼び出すことに注意してください。

于 2013-01-06T00:49:53.623 に答える