老实说,我什至不知道从哪里开始解决这个问题,但我已经在我的网站上实现了一个“喜欢”功能,当我查看控制台时,我看到了一个非常长的 SQL 语句。
我不会发布我所有的 dev.log,因为它太长了,但这里有一些错误的片段..
Started POST "/recipes/1/like?like=true" for 127.0.0.1 at 2015-11-20 22:46:45 -0500
Processing by RecipesController#like as HTML
Parameters: {"authenticity_token"=>"b64tIveTCtassgzKoJ/d65c72b3DfLmkC1ddQrCRBsbxnAuKMYpnz8L/+m5SsJ8to57v4sFXSkLIZB9QdFXdTQ==", "like"=>"true", "id"=>"1"}
^[[1m^[[35mRecipe Load (0.1ms)^[[0m SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT 1 [["id", 1]]
^[[1m^[[36mCACHE (0.0ms)^[[0m ^[[1mSELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT 1^[[0m [["id", "1"]]
^[[1m^[[35mCACHE (0.0ms)^[[0m SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT 1 [["id", "1"]]
然后它只是在大约 10,000 行上重复该 sql 语句,然后切换到这个错误
15008 app/controllers/recipes_controller.rb:46:in `like'
15009 app/controllers/recipes_controller.rb:46:in `like'
15010 app/controllers/recipes_controller.rb:46:in `like'
15011 app/controllers/recipes_controller.rb:46:in `like'
15012 app/controllers/recipes_controller.rb:46:in `like'
15013 app/controllers/recipes_controller.rb:46:in `like'
15014 app/controllers/recipes_controller.rb:46:in `like'
15015 app/controllers/recipes_controller.rb:46:in `like'
15016 app/controllers/recipes_controller.rb:46:in `like'
15017 app/controllers/recipes_controller.rb:46:in `like'
我有点卡住了,我希望有人能指出正确的方向。
这是我的代码:
控制器
def like
@recipe = Recipe.find(params[:id])
Like.create(like: params[:like], chef: Chef.first, recipe: @recipe)
flash[:success] = "Your selection was successful"
redirect_to :back
end
模型:
class Like < ActiveRecord::Base
belongs_to :chef
belongs_to :recipe
end
我的其他控制器中有相应的“has_many””
移民:
class CreateLikes < ActiveRecord::Migration
def change
create_table :likes do |t|
t.boolean :like
t.integer :chef_id, :recipe_id
t.timestamps
end
end
end
看法:
<%= link_to like_recipe_path(@recipe, like: true), method: :post do %>
<i class="glyphicon glyphicon-thumbs-up"></i>
<% end %>