Sql 在RubyonRails中查询关系并通过Ajax更新结果
我有一个ROR应用程序,它有许多玩家和许多推荐的游戏。游戏显示在一个提要上,玩家可以决定从这个提要中隐藏它们。隐藏函数的工作原理如下: 在player.rb中:Sql 在RubyonRails中查询关系并通过Ajax更新结果,sql,ruby-on-rails,ajax,relationships,Sql,Ruby On Rails,Ajax,Relationships,我有一个ROR应用程序,它有许多玩家和许多推荐的游戏。游戏显示在一个提要上,玩家可以决定从这个提要中隐藏它们。隐藏函数的工作原理如下: 在player.rb中: has_many :hides, :foreign_key=> "hider_id", :dependent => :destroy has_many :hidees, :through => :hides def hidden?(hidee) hides.
has_many :hides, :foreign_key=> "hider_id",
:dependent => :destroy
has_many :hidees, :through => :hides
def hidden?(hidee)
hides.find_by_hidee_id(hidee)
end
def hide!(hidee)
hides.create!(:hidee_id => hidee.id)
end
def unhide!(hidee)
hides.find_by_hidee_id(hidee).destroy
end
隐藏控制器.rb
class HidesController < ApplicationController
def create
@game = Game.find(params[:hide][:hidee_id])
current_profile.hide!(@game)
redirect_to :back
end
def destroy
@game = Hide.find(params[:id]).hidee
current_profile.unhide!(@game)
redirect_to :back
end
end
routes.rb
resources :games do
member do
post :publish
post :unpublish
get :view
get :hidees, :hiders
end
我试着做两件事:1。写一个函数,如果游戏和玩家之间的隐藏关系存在,我可以从提要中隐藏游戏,以及2。写一个“显示隐藏”按钮,允许我返回玩家“隐藏”的所有项目
到目前为止,第1部分。我在视图中有以下代码,虽然这在设置关系方面起到了作用,但它不会从提要中“隐藏”游戏——我猜我需要ajax来实现这一点
- if current_profile.hidden?(game)
= form_for current_profile.hides.find_by_hidee_id(game), :html => { :method => :delete } do |f|
= f.submit "Unhide", :title => "Unhide this game."
- else
= form_for current_profile.hides.build(:hidee_id => game.id) do |f|
= f.hidden_field :hidee_id
= f.submit "Hide", :title => "Hide this game"
非常感谢您观看这篇文章,我知道它很长,但如果您能提供任何帮助,我将不胜感激。还有,谢谢你抽出时间
resources :games do
member do
post :publish
post :unpublish
get :view
get :hidees, :hiders
end
- if current_profile.hidden?(game)
= form_for current_profile.hides.find_by_hidee_id(game), :html => { :method => :delete } do |f|
= f.submit "Unhide", :title => "Unhide this game."
- else
= form_for current_profile.hides.build(:hidee_id => game.id) do |f|
= f.hidden_field :hidee_id
= f.submit "Hide", :title => "Hide this game"