0

我的投票链接通常有效,除非链接是通过 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的制造商,所以我仍在调试。

4

2 回答 2

0

解决方案非常简单。首先运行 rake 路由并确保以正确的格式提供路由请求的参数。

在我的情况下,参数应该是 vote_car_manufacturer _path(@car.id, id: Manufacturer, type: "up")

于 2013-03-02T18:37:42.393 回答
0

你谈论ajax调用,但是

:remote => true 

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

于 2013-02-24T23:45:49.700 回答