辅助方法未触发

Helper method not triggering

我正在开发一个应用程序,您可以在其中将游戏添加到库中,也可以将它们删除。我有通过单击按钮工作的添加功能,但是我的 if 语句没有显示“从库中删除”。

这是我的游戏控制器中的库方法,它控制 add/remove 功能:

def library
    type = params[:type]
    game = Game.new(game_params)
    game.fetch_data

    if type == "add"
      current_user.library_additions << game
      redirect_to user_library_path(current_user), notice: "Game was added to your library"

    elsif type == "remove"
      current_user.library_additions.delete(game)
      redirect_to root_path, notice: "Game was removed from your library"
    else
      # Type missing, nothing happens
      redirect_to game_path(game), notice: "Looks like nothing happened. Try once more!"
    end

在视图中,“添加到库”按钮应该出现在不在您的库中的游戏上,如果它在您的库中,它应该切换到“从库中删除”

<% if user_added_to_library?(current_user, game) %>

            <button type="button"><%= link_to 'Remove from library', add_game_path(game.id, type: "remove", game: game), method: :put %> </button>

          <% else %>
           <button type="button"> <%= link_to 'Add to library', add_game_path(game.id, type: "add", game: game), method: :put %> </button>
          <% end %>

user_added_to_library决定的动作?不工作,所以我总是看到“添加到库”按钮。

这是我 user_added_to_library 的助手?

module GamesHelper
  def user_added_to_library? user, game
    user.libraries.where(user: user, game: @game).any?
  end
end

我想也许我需要将库更改为 library_additions,但我收到 StatementInvalid 错误。现在编写代码的方式不会抛出错误,但也可能根本不存在。

如果需要,我的用户模型:

class User < ApplicationRecord

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :games
  has_many :libraries
  has_many :library_additions, through: :libraries, source: :game
end

我需要改变我的 user_added_to_library 吗?方法还是有其他问题?

房间里的大象实际上是此代码的一组总体设计问题。这种设计与 RESTful 相去甚远,并且违反了 HTTP 动词的语义(PUT 不应删除资源)并且使用一个方法来完成许多不同的工作(创建和销毁资源)真的很臭。你甚至没有检查游戏是否真的保存了。

应使用 DELETE 请求销毁资源。在 Rails 中,您可以通过简单地正确使用 HTTP 动词来创建和修改资源:

POST    /games      # create a game
PATCH   /games/:id  # update a game
DELETE  /games/:id  # destroy a game

大多数情况可以而且应该由 standard CRUD routes generated by the resources macro. If you have resources with relationships you describe those relationships with nested routes. In this case you might choose to nest the route in a singular resource 处理,因为您是来自当前用户的 adding/removing 游戏。

# generates 
# POST     /user/games
# DELELE   /user/games/:id
resource :user, only: [] do
  resources :games, only: [:create, :destroy]
end 

这将由 GamesController 中的 #create#destroy 方法处理。

第二个问题实际上是您的数据库设计和模型。如果您想创建一个设计,让用户拥有可以组织到不同库中的游戏,您可以通过以下方式实现:

class User < ApplicationRecord
  has_many :libraries
  has_many :games, through: :libraries
end

class Library < ApplicationRecord
  belongs_to :user
  has_many :library_games
  has_many :games, through: :library_games
end 

class LibraryGame < ApplicationRecord
  belongs_to :library
  belongs_to :game
  has_one :user, through: :library 
end

class Game < ApplicationRecord
  has_many :library_games
  has_many :libraries, through: :library_games
  has_many :users, through: :libraries
end

在树上设置间接关联可以通过以下方式检查用户是否有游戏:

class User < ApplicationRecord
  has_many :libraries
  has_many :games, through: :libraries

  def has_game?(game)
    games.where(id: game.id).exist?
  end 
end

这根本就没有理由涉及辅助方法。毕竟你真的只是在问用户对象一个问题。这不应该涉及将两个不同的对象传递给一个单独的方法。