老实说,我甚至不知道从哪里开始解决这个问题但是我已经在我的网站上实现了“喜欢”功能,当我在看控制台时,我看到了一个非常真实的长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 %>
答案 0 :(得分:0)
根据上面的信息,我认为我能得出的唯一结论是代码redirect_to :back
正在调用一个动作,它将再次调用动作like
,这可能导致循环。
例如,如果您正在调用“喜欢”操作并且“喜欢”操作重定向回“喜欢”操作,则会陷入循环。
尝试渲染页面,如果你想留在同一页面或redirect_to:an_explicit_action你设置你的变量,然后渲染你想要的页面。