2014-11-15 60 views
0

我正在嘗試構建一個自定義控制器操作,它將從控制器相關的coffeescript文件中的ajax接收參數。沒有路由匹配使用Ajax定製控制器操作

但是控制檯筆記誤差404和無法訪問路線:如何配置這使路由和行動工作

enter image description here

?在campaigns_controller

控制器動作:

def create_campaign_location_relationship 
    @location = Location.find(params[:location_id]) 
    @campaign = Campaign.find(params[:id]) 

    @insert = CampaignLocation.new(campaign_id: @campaign.id, 
            location_id: @location.id) 
    @insert.save 
end 

阿賈克斯在campaigns.js

$('[name=commit]').bind "click", -> 
    # Insert the code to allow for a user to 
    alert "Relationships created" 

    selectedLocations = root.table.rows(".selected").data() 
    for locationSelected in selectedLocations 
     location_id = locationSelected[0] 
     $.ajax({ 
     url: "create_campaign_location_relationship/" + location_id, 
     type: "post", 
     dataType: "json" 
     }) 

    return 

活動路線:

resources :campaigns, only: [:create, :edit, :update, :destroy, :show] 

    resources :campaigns do 
    member do 
     match "/create_campaign_location_relationship/:location_id", to: "campaigns#create_campaign_location_relationship", via: 'post' 
    end 
    end 

回答

0

你誤會_method PARAM。要解決您發出更改AJAX REQ:

$.ajax({ 
    url: "/create_campaign_location_relationship" + location_id, 
    type: "post", 
    dataType: "json" 
    }) 

使用_method這裏是沒有必要的。還請注意,您已將create_campaign_location_relationship定義爲get,而它必須是post

_method是一個隱藏字段,通常在表單標籤中使用,並通過form_forform_tag方法自動生成。在處理jQuery $.ajax({})方法時,沒有必要使用它。另外,_method取值patch/put/destroy

+0

在路由中,我將'get'更改爲'post'和ajax中的更改,我仍然收到控制檯 – Sauron

+0

中的錯誤404,以及錯誤描述是什麼? – blelump

+0

再次出現錯誤404,找不到 – Sauron

相關問題