簡體   English   中英

僅當使用ajax時,rails路由錯誤

[英]rails routing error only when ajax used

我的投票鏈接通常有效,除非該鏈接是通過ajax部分呈現的。 通過JavaScript局部渲染時,出現路由錯誤,該錯誤在Firebug中可見,否則沒有錯誤。

沒有螢火蟲,我知道會出現錯誤,因為在嘗試添加新制造商時未添加制造商。 如果我手動刷新頁面,則鏈接和新的制造商存在並且可以進行投票。

因此,基本上使用ajax會破壞投票鏈接。

耙路:

vote_car_manufacturer POST   /cars/:car_id/manufacturers/:id/vote(.:format)                manufacturers#vote

視圖:

<%= link_to '+', vote_car_manufacturer _path(car_id: @car.id, manufacturer_id: manufacturer.id, type: "up"), method: "post" %>

螢火蟲:

No route matches {:action=>"vote", :controller=>"manufacturers", :car_id=>1, :manufacturer_id=>29, :type=>"up"}

使用ajax時,我是否必須以不同的方式實現投票鏈接?

更新

通過反復試驗使它變得半成品。 已更改:

<%= link_to '+', vote_car_manufacturer _path(car_id: @car.id, manufacturer_id: manufacturer.id, type: "up"), method: "post" %>

至:

<%= link_to '+', vote_car_manufacturer _path(@car.id, manufacturer.id, "up"), :method => :post %>

現在它說找不到沒有ID的制造商,所以我仍在調試。

您談論的是ajax通話,但是

:remote => true 

不在link_to中 ,如文檔中所指定: http : //api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

解決方案很簡單。 首先運行rake路由,並確保以正確的格式提供路由請求的參數。

在我的情況下,參數應該是vote_car_manufacturer _path(@ car.id,id:制造商,類型:“ up”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM