【问题标题】:How do I send two parameters with one link in Rails?如何在 Rails 中通过一个链接发送两个参数?
【发布时间】:2014-12-01 16:19:33
【问题描述】:

我正在使用 Rails evoting 应用程序。

我有以下模型:User、Election、Contestant。

我希望允许多个用户在特定选举中为参赛者投票,当用户在该选举中投票时,应隐藏指向每个用户的链接。

这个关联太复杂了,我弄不明白。

这些是我的联想:

class User < ActiveRecord::Base

  has_many :contestants
  has_many :contestant_votes

end

class Contestant < ActiveRecord::Base
 mount_uploader :avatar, AvatarUploader
 belongs_to :user
 has_many :contestant_votes
 has_many :elections

 def votes
    read_attribute(:votes) || contestant_votes.sum(:value)
 end

end

class Election < ActiveRecord::Base
  has_many :contestants
end

如果当前用户在选举中为某位选手投票,我如何隐藏投票链接?

【问题讨论】:

  • 您可以在用户模型中添加一个“已投票”标志来指示用户是否已经投票。并且链接隐藏应该在控制器中完成。
  • 好吧,如果有新的选举呢?我建议投票的文件仍然是错误的

标签: ruby ruby-on-rails-4 activerecord model-view-controller associations


【解决方案1】:

您可以在contestant_votes 上添加唯一性约束。然后你可以添加一个逻辑来检查当前用户和参赛者之间是否存在contestant_vote

【讨论】:

  • 好吧,我不完全明白你的意思,请你给我一些代码示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多