我需要资源路由和 :id 访问方面的帮助
我如何从 like_book_path(@book),:method=>:post
中使用类似方法访问图书 ID。如您所见,此链接位于图书循环下。我需要什么如何使 like_book_path(@book)
携带 book_id 以及如何在类似操作中访问它? 到目前为止,我可以通过 book.id
获取 id。任何帮助将不胜感激。
视图:
<% @books.each do |book|%>
<%= book.name%>
<%= link_to "like this book", like_book_path(@book), :method => :post %>
<%end%>
图书控制器:
def like
@book = Book.find(params[:id])
#other methods to facilitate like functionality
end
路线:
resources :book do
member do
post :like
end
end
How do i access the book id in like method from like_book_path(@book),:method=>:post
.As you can see,this link is under the book loop.What do i need to do to make like_book_path(@book)
carry the book_id and how would i access it in like action?
so far in the view i can get the id through book.id
.Any help will be greately appreciated.
View:
<% @books.each do |book|%>
<%= book.name%>
<%= link_to "like this book", like_book_path(@book), :method => :post %>
<%end%>
Book controller:
def like
@book = Book.find(params[:id])
#other methods to facilitate like functionality
end
Route:
resources :book do
member do
post :like
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你应该用以下内容替换你的循环:
区别只是使用
book
而不是不存在的@book
。I guess you should replace your loop with:
the difference is just to use
book
instead of the non existing@book
.