2
我想了解如何使用索引頁中控制器中定義的方法。 我試圖在我的博客上實現「喜歡」按鈕。Rails:在索引文件中使用PostController方法的正確方法
PostController中
def like
@post = Post.find(params[:id])
@post.like += 1
@post.save
end
在所有的職位都列出的指標,我想是這樣的。
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.created_at.strftime("%Y/%m/%d, %I:%M%p") %></td>
<td><%= post.view %></td>
<td><%= link_to 'like', like_post_path %></td>
<td>hate</td>
</tr>
<% end %>
我通過看代碼上心,
<%= link_to 'make a new post', new_post_path %>
<%= link_to 'Edit', edit_post_path(post) %>
我想使用方法,在索引頁控制器的方式是
(在PostController中的方法)_post_path,但似乎我錯了。
undefined local variable or method `like_post_path'
我也試着像(崗位)。
我的最終目標是使這一功能作爲Ajax功能,所以我希望它是像
<% link_to_function 'like', like_post, remote: true %>
形式有什麼用「像」,在這種情況下,該方法的正確方法嗎?
它的工作。謝謝。你能否進一步解釋「成員做」部分?另外,你能告訴我如何「編輯」和「新」的方法是這樣路由,但沒有顯示在config/routes.rb文件? –
'resources'是'edit'和'new'的宏,以及其他REST方法。在'resources'塊中,你可以定義「成員」路由,這些路由通過id創建指向單個記錄的路由,而「收集」路由則不會。進一步的解釋可以在這裏找到(http://guides.rubyonrails.org/routing.html) – numbers1311407