我对Rails很新鲜,我似乎有类似的问题,但我无法解决我的问题。
我的路线:
resources :kindergartens do
member do
put"like",to:"kindergartens#upvote"
end
member do
post :join
post :quit
end
我的模特:
acts_as_votable
我的控制器:
def upvote
@kindergarten = Kindergarten.find(params[:id])
@kindergarten.upvote_by current_user
redirect_to :back
end
我的index.html:
<%= link_to like_kindergarten_path(@kindergarten), method: :put do %>
<div class="pi-thumbs-up">
<i class="fa fa-thumbs-up fa-2x" , style="color:#ff5722;"></i>
<%= kindergarten.get_upvotes.size %>
</div>
<% end %>
show.html.erb中没有错误。但是无法工作 index.html.erb。
我得到了同样的错误:
ActionController::UrlGenerationError in Kindergartens#index
Showing /Users/liudezheng/Dropbox/未命名文件夹/Lyn-add-
votes/app/views/kindergartens/index.html.erb where line #66 raised:
No route matches {:action=>"upvote", :controller=>"kindergartens",
:id=>nil} missing required keys: [:id]
答案 0 :(得分:0)
因为它是index action
,我认为你必须循环`@kindergartens,
所以,根据 <%= kindergarten.get_upvotes.size %>
一行,循环的参数是 kindergarten
而不是 @kindergarten
强>
所以,它应该是
<%= link_to like_kindergarten_path(kindergarten), method: :put do %>
<div class="pi-thumbs-up">
<i class="fa fa-thumbs-up fa-2x" , style="color:#ff5722;"></i>
<%= kindergarten.get_upvotes.size %>
</div>
<% end %>
PS:分享您的index action
和完整的index.html
以获得更多相关答案。
答案 1 :(得分:0)
nil
内的 # kindergarten_controller.rb
def index
@kindergartens = Kindergarten.all
end
为# kindergartens/index.html.erb
<%= @kindergartens.each do |kindergarten|
<%= link_to like_kindergarten_path(kindergarten), method: :put do %>
<div class="pi-thumbs-up">
<i class="fa fa-thumbs-up fa-2x" , style="color:#ff5722;"></i>
<%= kindergarten.get_upvotes.size %>
</div>
<% end %>
。
要解决此问题,您可能希望执行以下操作之一:
serve
您的HTML:
--reload-debounce
我希望有所帮助。