2017-01-11 20 views
3

レールでクロスプラットフォーム要求を実行しようとしています。Rails CORS:ActionController :: RoutingError([OPTIONS] "/ batch"と一致するルートはありません):

私のjqueryのコードは以下の通りです: - :

def new 
    @batch = Batch.new 
    respond_to do |format| 
     format.json 
     format.html 
    end 
    end 

    def create 
    @batch = Batch.new(batch_param) 
    respond_to do |format| 
     if @batch.save 
     format.json { render json: @batch, status: :created, location: @batch } 
     format.html { redirect_to @batch, notice: "Save process completed!" } 
     else 
      format.html { 
      flash.now[:notice]="Save proccess coudn't be completed!" 
      render json: @batch.errors, status: :unprocessable_entity 
      } 
      format.json { render json: @batch.errors, status: :unprocessable_entity} 
     end 
    end 
    end 
def batch_param 
     params.require(:batch).permit(:name, :course_id, :start_date, :end_date, :status) 
    end 

しかし、私は、フォームを使用してレコードを追加しようとするたびに、それは、以下のエラーが表示さ -

<script> 
    $.ajaxSetup({ 
     headers: { 
     'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') 
     } 
    }); 
    $(document).ready(function() { 
     $('#submit-button').click(function() { 
      $.ajax({ 
       type: "POST", 
       url: "http://localhost:3000/batches", 
       beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))}, 
       xhrFields: { 
        withCredentials: true 
       }, 
       data: {  
        batch: { 
         name: $("#name").val(), 
         course_id: $("#course_id").val(), 
         start_date: $("#start_date").val(), 
         end_date: $("#end_date").val(), 
         status: $("#batch_status").val(), 
         } 
       }, 
       dataType: "JSON", 
       error: function(error) { 
        console.log(error); 
         }, 
       success: function(data) { 
         console.log(data); 
         return false; 
         }, 
      }) 
     }); 
    }) 
</script> 

そして、私のコントロールは以下の通りですログレール: -

エラーを私は私の最初のデータを提出する際

Started OPTIONS "/batches" for 127.0.0.1 at 2017-01-11 22:02:49 +0545 
    ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations" 

ActionController::RoutingError (No route matches [OPTIONS] "/batches"): 

私はより多くの1時間のデータを提出するエラー: - 誰もがこの問題で私を助けることができる

Started OPTIONS "/batches" for 127.0.0.1 at 2017-01-11 22:08:50 +0545 

ActionController::RoutingError (No route matches [OPTIONS] "/batches"): 

を。

答えて

2

CORSをサポートするようにRailsを設定する必要があります。

1つの解決策は、rack-cors gemを使用することです。

のconfig/application.rb

module YourApp 
    class Application < Rails::Application 

    # ... 

    # Rails 3/4 

    config.middleware.insert_before 0, "Rack::Cors" do 
     allow do 
     origins '*' 
     resource '*', :headers => :any, :methods => [:get, :post, :options] 
     end 
    end 

    # Rails 5 

    config.middleware.insert_before 0, Rack::Cors do 
     allow do 
     origins '*' 
     resource '*', :headers => :any, :methods => [:get, :post, :options] 
     end 
    end 

    end 
end 
+0

うん、それは働いたが、それは今ではActionController :: InvalidAuthenticityToken(ActionControllerは言う...新しい問題に上げたに追加する必要になります切り取ら: :InvalidAuthenticityToken): –

+0

本物のトークンを正しく渡していることを確認する必要があります。これをチェックしてください:http://stackoverflow.com/questions/7203304/warning-cant-verify-csrf-token-authenticity-rails –

+0

申し訳ありませんが、私はすでに試してみました... –

関連する問題