尝试将收藏夹模型添加到我的应用程序...获取路由错误

时间:2013-02-26 01:31:46

标签: ruby-on-rails

我有一个应用程序,其中有一个用户可以查看的预定义饮料列表。我试图让用户能够喜欢/不喜欢喝一杯。

由于某些原因,当我点击收藏链接时,我收到了一个AssociationMismatch错误。显然代码不喜欢我的current_user.favorite_drinks<< @drink部分代码

CONTROLLER drinks_controller.rb

def favorite
  type = params[:type]
  if type == "favorite"
    @drink = Drink.find params[:drink_id]
    current_user.drinks << @drink
      redirect_to :back, notice: 'You favorites #{@drink.name}'
    elsif type == "unfavorite"
      current_user.drinks.delete(@drink)
      redirect_to :back, notice: 'You unfavorited #{@drink.name}'
    else
    redirect_to :back, notice: 'Nothing happened'
  end
end

CONTROLLER favorites_controller.rb

def show
  @user = User.find(params[:id])
  if @user
    @drinks = @user.favorite_drinks.all
    render action: :show
  else
    render file: 'public/404', status: 404, formats: [:html]
  end
end

ROUTES routes.rb

将'auth /:provider / callback'匹配为:'sessions#create'   匹配'auth / failure',to:redirect('/')   匹配'signout',to:'sessions#destroy',as:'signout'

root to:“drink #index”   资源:眼镜   资源:成分   资源:内阁   资源:饮料     获得'最爱',:on =&gt; :采集   结束   资源:收藏夹

获取“收藏/展示”

MODEL user.rb

     has_one :cabinet
     has_many :favorite_drinks
     has_many :drinks, through: :favorite_drinks

MODEL favorite_drink.rb

    attr_accessible :drink_id, :user_id

    belongs_to :user
    belongs_to :drink

查看_results.html.haml

   %td= link_to "favorite", favorite_drinks_path(drink_id: drink.id, type: "favorite"), method: "get"
   %td= link_to "unfavorite", favorite_drinks_path(drink, type: "unfavorite"), method: "get"

查看收藏夹/ show.html.haml

   %table.table.table-striped
     %thead
       %tr
         %th Name
     %tbody
       - if @drinks.each do |drink|
         %tr
           %td= link_to drink.name, drink

我的更新代码

迁移create_favorite_drinks.rb

    class CreateFavoriteDrinks < ActiveRecord::Migration
     def change
       create_table :favorite_drinks do |t|
         t.integer :drink_id
         t.integer :user_id

         t.timestamps
       end
     end
   end

1 个答案:

答案 0 :(得分:3)

代码中那部分导致错误的两件事

  1. @drink是一个数组,所以在使用<<
  2. 将它们添加到用户喜爱的饮品列表之前,您需要遍历每个数组
  3. current_user.favorite_drinks&lt;&lt; @drink - 您正在将Drink对象推送到favorite_drinks表,这就是您不匹配的原因
  4. 最好的解决方案是像这样设置你的模型

    # user.rb
    has_many :favorite_drinks
    has_many :drinks, through: :favorite_drinks
    
    # favorite_drink.rb
    belongs_to :user
    belongs_to :drink
    
    # drink.rb
    has_many :favorite_drinks
    has_many :users, through: :favorite_drinks
    

    这假设user_id表上有drink_idfavorites_drinks。然后你可以使用current_user.drink_ids = params[:drink_ids]

    更新:

    我刚刚注意到您正在使用params[:drink_id]。所以将控制器代码更改为

    @drink = Drink.find params[:drink_id]
    current_user.drinks << @drink
    

    你应该没事。

    更新:将@drink移出if块,以便在类型不受欢迎时仍然可以访问它

    @drink = Drink.find params[:drink_id]
    
    if type == "favorite"
      current_user.drinks << @drink
      redirect_to :back, notice: 'You favorites #{@drink.name}'
    elsif type == "unfavorite"
      current_user.drinks.delete(@drink)
      redirect_to :back, notice: 'You unfavorited #{@drink.name}'
    else
      redirect_to :back, notice: 'Nothing happened'
    end