竖起大拇指投票 gem 的多态类

发布于 2024-10-15 06:56:51 字数 1153 浏览 1 评论 0原文

我想为许多不同的控制器制作一个通用的投票控制器。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腻橙味 2024-10-22 06:56:51

做了类似的事情

  def create
    voteable_class = params[:voteable_type].constantize
    voteable_id = (params[:voteable_type].downcase + "_id").to_sym
    voteable_instance = voteable_class.find(params[voteable_id])
    current_user.vote(voteable_instance, :direction => params[:thumb_direction], :exclusive => true)
    redirect_to :back
  end

,并更改了我想使用的每个模型上嵌套投票资源的路线。

埃尔布

<%= form_tag [voteable, Vote.new] do |f| %>
    <%= radio_button_tag :thumb_direction, :up %>
    <%= radio_button_tag :thumb_direction, :down %>
    <%= hidden_field_tag :voteable_type, voteable.class %>
    <%= submit_tag :vote %>
<% end %>

Did something like this

  def create
    voteable_class = params[:voteable_type].constantize
    voteable_id = (params[:voteable_type].downcase + "_id").to_sym
    voteable_instance = voteable_class.find(params[voteable_id])
    current_user.vote(voteable_instance, :direction => params[:thumb_direction], :exclusive => true)
    redirect_to :back
  end

And changed my routes for the nested voting resource on each model I wanted to use it.

erb

<%= form_tag [voteable, Vote.new] do |f| %>
    <%= radio_button_tag :thumb_direction, :up %>
    <%= radio_button_tag :thumb_direction, :down %>
    <%= hidden_field_tag :voteable_type, voteable.class %>
    <%= submit_tag :vote %>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文