竖起大拇指投票 gem 的多态类
我想为许多不同的控制器制作一个通用的投票控制器。
我正在使用 Thumbs_up gem,它以前是 vote_fu gem。
https://github.com/kitop/thumbs_up/blob/master/lib /acts_as_voter.rb
我的表单看起来像这样,它是对象 @voteable 的一部分:
<strong class="result">Votes: <%= voteable.votes_for - voteable.votes_against %></strong>
<%= form_tag user_votes_path(current_user) do |f| %>
<%= radio_button_tag :thumb_direction, :up %>
<%= radio_button_tag :thumb_direction, :down %>
<%= hidden_field_tag :voteable, @voteable %>
<%= submit_tag :vote %>
<% end %>
但是,当我尝试将可投票对象直接传递到控制器中时,它不起作用。
未定义方法“base_class” 字符串:类
我的问题是如何多态地查找同一个对象...即传递 voteable_type 和 _id 而不是对象本身...除非还有其他更简单的方法?
控制器看起来像这样
def create
#@user = User.find(params[:user_id])
current_user.vote(params[:voteable], :direction => params[:thumb_direction], :exclusive => true)
end
#routes
resources :users do
resources :votes
end
I want to make a generic voting controller for lots of different controllers.
I'm using the Thumbs_up gem which was formerly the vote_fu gem.
https://github.com/kitop/thumbs_up/blob/master/lib/acts_as_voter.rb
My form looks like so which is a partial with the object @voteable:
<strong class="result">Votes: <%= voteable.votes_for - voteable.votes_against %></strong>
<%= form_tag user_votes_path(current_user) do |f| %>
<%= radio_button_tag :thumb_direction, :up %>
<%= radio_button_tag :thumb_direction, :down %>
<%= hidden_field_tag :voteable, @voteable %>
<%= submit_tag :vote %>
<% end %>
However when I try passing the voteable object into the controller directly it doesn't work.
undefined method `base_class' for
String:Class
My question is then how to polymorphically look up the same object... ie pass the voteable_type and _id instead of the object itself... Unless there is some other easier way?
Controller looks like this
def create
#@user = User.find(params[:user_id])
current_user.vote(params[:voteable], :direction => params[:thumb_direction], :exclusive => true)
end
#routes
resources :users do
resources :votes
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做了类似的事情
,并更改了我想使用的每个模型上嵌套投票资源的路线。
埃尔布
Did something like this
And changed my routes for the nested voting resource on each model I wanted to use it.
erb