看到一些參考,但它的舊的Rails 2解決方案。有足夠的時間來理解Rails 3的一些術語。無法添加方法和link_to工作(鐵路3)
我在我的控制器中添加了一個'dndl'方法。 我在我的索引中添加了一個link_to。 我試了一次又一次把航線
Controller:
def dnld
blah blah
end
指數:
<td><%= link_to 'Show', stock %></td>
<td><%= link_to 'Edit', edit_stock_path(stock) %></td>
<td><%= link_to 'Dnld', dnld, {:action => 'dnld'} %></td>
<td><%= link_to 'Destroy', stock, :confirm => 'Are you sure?', :method => :delete %>
路線:
resources :stocks do
collection do
put 'dnld'
end
end
我已經試過:
<td><%= link_to 'Dnld', stock, {:action => 'dnld'} %></td>
# End up on the stock show page with dnld not executed to my knowledge
<td><%= link_to 'Dnld', , {:action => 'dnld'} %></td>
# Produces an error
<td><%= link_to 'Dnld', dnld_stock_path(stock), {:action => 'dnld'} %></td>
# It doesn't know what dnld_stock_path is, yet I don't understand why it DOES know what edit_stock_path is and cannot find documentation to explain this.
感謝幫助!
我還需要上面的kishi的答案。我使用了會員電話。但是我認爲:as => dnld從上面的kishi是必要的。在link_to中,我使用了:<%= link_to'Dnld',dnld_stock_path(stock)%>這就是我所需要的URL */stocks/2/dnld。從PUT改爲GET也能治癒我。我不完全明白爲什麼(小白),但我得到了我想要的結果。不要忘記tor「耙路」。 – Dwarfer 2011-04-03 17:02:52