0
好的,我正在嘗試在Ruby中創建一個reddit樣式頁面,其中用戶可以提交鏈接,然後upvote/downvote它們。我爲鏈接生成了一個腳手架,這些鏈接基本上像博客教程一樣工作,然後自定義編寫了一個遷移程序,將一個vote_count:integer列添加到鏈接表。然後,我進入控制器一個upvote(增加links.vote_count)和downvote(它遞減links.vote_count)的附加方法,並在index.erb.html頁面顯示所有鏈接,我想吱吱「link_to」調用這些方法的按鈕。我現在有東西的方式,雖然我得到的錯誤:無法找到沒有ID的鏈接。通過控制器方法更新表格中的值
links_controller.rb的有關部分
class LinksController < ApplicationController
before_action :set_link, only: [:show, :edit, :update, :destroy]
# GET /links
# GET /links.json
def index
@links = Link.all
end
def upvote
@link = Link.find(params[:id])
@link.vote_count += 1
end
def downvote
@link = Link.find(params[:id])
@link.vote_count -= 1
end
index.html.erb
<tbody>
<% @links.each do |link| %>
<tr>
<td><%= link.vote_count %></td>
<td><%= link_to 'Up', upvote_links_path(link) %></td>
<td><%= link_to 'Down', downvote_links_path(link) %></td>
<td><%= link.title %></td>
<td><%= link.url %></td>
<td><%= link.user_id %></td>
<td><%= link_to 'Show', link %></td>
<td><%= link_to 'Edit', edit_link_path(link) %></td>
<td><%= link_to 'Destroy', link, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
的routes.rb
resources :links do
collection do
get :upvote
get :downvote
end
end
的
相關部分是否有更新這更簡單的方法表值或我做錯了什麼?
'POST'會少機器人更好地將票投:) –
@BillyChan同意 –
因爲我是比較新的軌道,你能解釋一下成員和收集之間的區別? – QuisEs99