2016-10-16 5 views
0

私は多くのポジションを持ち、各ポジションに多くの動きがあるユーザーがあるポートフォリオを持つアプリを持っています。したがって、特定の位置に関する関連する動き指標ページのURLは、portfolio_position_movementsのようになります。私はインデックスページを持っており、コントローラのアクションは、ファイルがこれです私のmovements.jsでAJAX URL値にレールルートを渡す

def index 
    @movements = @position.movements.all 
    respond_to do |format| 
    format.html 
    format.json { render json: @movements} 
    end 
end 

私のAJAX呼び出しのようになります。私は、この意志ダイナミックルートパスを渡すことができますどのように

var loadData = function(){ 
      $.ajax({ 
       type: 'GET', 
       contentType: 'application/json; charset=utf-8', 
       url: ?, 
       dataType: 'json', 
       success: function(data){ 
       drawBarPlot(data); 
       }, 
       failure: function(result){ 
       error(); 
       } 
      }); 
      }; 

任意の位置オブジェクトの移動インデックスで作業しますか?

答えて

0

私は、次のようにそれをやった私にとっては、JSファイルにERBタグを使用することができます。

#edit.js.erb 

$modal = $('.modal'), 
$modalBody = $('.modal .modal-body'), 
$modalHeading = $('.modal .modal-heading'); 
$modalHeading.html("Edit <%= @student.full_name.titleize %>'s information"); 
$modalBody.html("<%= escape_javascript(render 'edit_student') %>"); 
$modal.modal(); 

注:ファイルの拡張子は.js.erbので、レールがそれを処理することができます。私は、モーダルフォームとstudents_controller.rbで編集する方法を呼んでいたことだった。

def edit 
    @student = Student.find(params[:id]) 
    respond_to do |format| 
     format.html # edit.html.erb 
     format.js # edit.js.erb 
     format.json { render json: @student } 
    end 
    end 

編集:

<script> 
var loadData = function(){ 
      $.ajax({ 
       type: 'GET', 
       contentType: 'application/json; charset=utf-8', 
       url: <%= my_ajax_path %>, 
       dataType: 'json', 
       success: function(data){ 
       drawBarPlot(data); 
       }, 
       failure: function(result){ 
       error(); 
       } 
      }); 
      }; 
</script> 

あなたのようなhtml.erbと利用レールルート内のJSコードを埋め込むことができます

my_ajax_pathとは何ですか?

1-このようなstudents_controllers.rbでメソッドを定義した:

は私は次のようでしたので、学生はAJAXを使用して適用することができ、使用可能なすべてのセクションのリストを必要とする例えばroutes.rbで定義されたレールのルートです:

def available_sections 
    batch_id = (params[:batch_id].nil? || params[:batch_id].empty?) ? 0 : params[:batch_id].to_i 

    if batch_id == 0 
     @sections = [].insert(0, "Select Section") 
    else 
     batch = Batch.find(params[:batch_id]) 
     # map to name and id for use in our options_for_select 
     @sections = batch.sections.map{|a| [a.section_name, a.id]} 
    end 
    end 

2- new.html.erb内部routes.rb

resources :students do 
    collection do 
    get :available_sections 
    post :create_multiple 
    end 
end 

-3-でそれへのルートを追加しました:

<script type="text/javascript"> 
$(document).ready(function() { 
    $('.student_section_id').hide(); 
    $('#student_batch').change(function() { 
     $.ajax({ 
      url: "/students/available_sections", 
      data: { 
       batch_id : $('#student_batch').val() 
      }, 
      dataType: "script", 
      success: function() { 
       if (+$('#student_batch').val() > 0) 
       { 
        $('.student_section_id').fadeIn(); 
       } 
       else 
       { 
        $('.student_section_id').fadeOut(); 
       } 
      } 
     }); 
    }); 
}); 
</script> 

その汚いコードを忘れる:それは私の最初のステップだったが、あなたはポイントを取得し、それがレールのルートを使用する必要がありますurl: "/students/available_sections"この行のためにあなたが得るために、コマンドラインからrails routesを呼び出すことによってそれを得ることができるようDすべてのアプリケーションルートのリスト

+0

私はmovements.jsファイルを持っています。どのようにしてそのファイルの値を取得できますか?あるいはhtmlファイルにスクリプトタグを置くだけですか? –

+0

my_ajax_pathはどこで定義しますか? –

+0

my_ajax_pathは 'routes.rb'で定義されるべきルートです –

関連する問題