Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未初始化的常量用户::FavoriteRecipe_Ruby On Rails_Ruby On Rails 3_Activerecord_Models_Favorites - Fatal编程技术网

Ruby on rails 未初始化的常量用户::FavoriteRecipe

Ruby on rails 未初始化的常量用户::FavoriteRecipe,ruby-on-rails,ruby-on-rails-3,activerecord,models,favorites,Ruby On Rails,Ruby On Rails 3,Activerecord,Models,Favorites,因此,我尝试为我的一个应用程序实现一个偏好系统,并遵循以下指南: 您描述的特定设置混合了几种类型的关联 A) 用户和配方 首先我们有一个用户模型,第二个是配方模型。每个配方都属于一个用户,因此我们有一个用户:拥有多个配方,配方属于:用户关联。此关系存储在配方的用户id字段中 $ rails g model Recipe user_id:integer ... $ rails g model User ... class Recipe < ActiveRecord::Base belo

因此,我尝试为我的一个应用程序实现一个偏好系统,并遵循以下指南:

您描述的特定设置混合了几种类型的关联

A) 用户和配方 首先我们有一个用户模型,第二个是配方模型。每个配方都属于一个用户,因此我们有一个用户:拥有多个配方,配方属于:用户关联。此关系存储在配方的用户id字段中

$ rails g model Recipe user_id:integer ...
$ rails g model User ...

class Recipe < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :recipes
end
添加此收藏夹有很多好处:通过与模型关联,我们可以得到最终结果

$ rails g model FavoriteRecipe recipe_id:integer user_id:integer

# Join model connecting user and favorites
class FavoriteRecipe < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :user
end

---

class User < ActiveRecord::Base
  has_many :recipes

  # Favorite recipes of user
  has_many :favorite_recipes # just the 'relationships'
  has_many :favorites, through: :favorite_recipes, source: :recipe # the actual recipes a user favorites
end

class Recipe < ActiveRecord::Base
  belongs_to :user

  # Favorited by users
  has_many :favorite_recipes # just the 'relationships'
  has_many :favorited_by, through: :favorite_recipes, source: :user # the actual users favoriting a recipe
end
在我们的Recipes控制器中,我们现在可以添加相应的收藏夹操作。在这里,我们需要确定用户想要做什么,喜欢还是不喜欢。为此,可以引入一个名为例如type的请求参数,稍后我们还必须将该参数传递给我们的link助手

class RecipesController < ...

  # Add and remove favorite recipes
  # for current_user
  def favorite
    type = params[:type]
    if type == "favorite"
      current_user.favorites << @recipe #stuck here :(
      redirect_to :back, notice: 'You favorited #{@recipe.name}'

    elsif type == "unfavorite"
      current_user.favorites.delete(@recipe)
      redirect_to :back, notice: 'Unfavorited #{@recipe.name}'

    else
      # Type missing, nothing happens
      redirect_to :back, notice: 'Nothing happened.'
    end
  end

end
类控制器<。。。
#添加和删除喜爱的食谱
#对于当前用户
def最爱
类型=参数[:类型]
如果类型==“收藏夹”

current_user.favorites是链接答案中的所有代码吗?如果是这样,你应该发布链接,而不是复制整个内容。我们知道如何点击链接。相反,当出现错误时,您应该发布代码、rails控制台的输出(格式化文本,而不是图像),等等。您的
FavoriteRecipe
类是否存在?它是否在
models
目录中名为
favorite\u recipe.rb
的文件中?如果没有,Rails将无法正确自动加载。是的,我的FavoriteRecipe类存在扫描您请共享您得到的异常的回溯。知道它来自哪一行确实很有帮助:current_user.favorites是链接答案中的所有代码吗?如果是这样,你应该发布链接,而不是复制整个内容。我们知道如何点击链接。相反,当出现错误时,您应该发布代码、rails控制台的输出(格式化文本,而不是图像),等等。您的
FavoriteRecipe
类是否存在?它是否在
models
目录中名为
favorite\u recipe.rb
的文件中?如果没有,Rails将无法正确自动加载。是的,我的FavoriteRecipe类存在扫描您请共享您得到的异常的回溯。知道它来自哪一行会很有帮助,确保它来自:current_user.favorites
##
# Association "A"

# Find recipes the current_user created
current_user.recipes

# Create recipe for current_user
current_user.recipes.create!(...)

# Load user that created a recipe
@recipe = Recipe.find(1)
@recipe.user

##
#  Association "B"

# Find favorites for current_user
current_user.favorites

# Find which users favorite @recipe
@recipe = Recipe.find(1)
@recipe.favorited_by # Retrieves users that have favorited this recipe

# Add an existing recipe to current_user's favorites
@recipe = Recipe.find(1)
current_user.favorites << @recipe

# Remove a recipe from current_user's favorites
@recipe = Recipe.find(1)
current_user.favorites.delete(@recipe)  # (Validate)
# config/routes.rb
resources :recipes do
  put :favorite, on: :member
end
class RecipesController < ...

  # Add and remove favorite recipes
  # for current_user
  def favorite
    type = params[:type]
    if type == "favorite"
      current_user.favorites << @recipe #stuck here :(
      redirect_to :back, notice: 'You favorited #{@recipe.name}'

    elsif type == "unfavorite"
      current_user.favorites.delete(@recipe)
      redirect_to :back, notice: 'Unfavorited #{@recipe.name}'

    else
      # Type missing, nothing happens
      redirect_to :back, notice: 'Nothing happened.'
    end
  end

end
<% if current_user %>
  <%= link_to "favorite",   favorite_recipe_path(@recipe, type: "favorite"), method: :put %>
  <%= link_to "unfavorite", favorite_recipe_path(@recipe, type: "unfavorite"), method: :put %>
<% end %>