Ruby on rails 4 Rails-实现一个;例如;功能和体验无限循环
老实说,我甚至不知道从哪里开始解决这个问题,但我已经在我的站点上实现了一个“like”功能,当我查看控制台时,我看到一个非常长的SQL语句 我不会发布我所有的dev.log,因为它太长了,但这里有一些错误的片段Ruby on rails 4 Rails-实现一个;例如;功能和体验无限循环,ruby-on-rails-4,ruby-2.2,Ruby On Rails 4,Ruby 2.2,老实说,我甚至不知道从哪里开始解决这个问题,但我已经在我的站点上实现了一个“like”功能,当我查看控制台时,我看到一个非常长的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 Paramet
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"]]
然后它只是在10000行上重复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'
我有点困了,我希望有人能指出正确的方向
这是我的代码:
class Like < ActiveRecord::Base
belongs_to :chef
belongs_to :recipe
end
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 %>
控制器
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
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 %>
类类似于
我的其他控制器中有相应的“has_many”
迁移:
class Like < ActiveRecord::Base
belongs_to :chef
belongs_to :recipe
end
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 %>
class
查看:
class Like < ActiveRecord::Base
belongs_to :chef
belongs_to :recipe
end
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 %>
根据上述信息,我认为我能得出的唯一结论是,代码
重定向到:back
正在调用一个动作,该动作将再次调用动作,就像
,这可能会导致循环
例如,如果您正在调用“like”操作,而该“like”操作正在重定向回“like”操作,它将陷入循环
如果要保持在同一页面上,请尝试呈现页面,或者将页面重定向到:一个显式的操作,在该操作中设置变量,然后呈现所需的页面。这似乎不够数据
like.create(like:params[:like],chef:chef.first,recipe:@recipe)
这个'like'变量/方法从何而来?它似乎是你的like模型,但你没有设置它。那么你可能没有发布整个代码片段吗?如果你能发布更多信息,它可能对我们有用。你能这样做吗?@BryanOemar HA!很好。你完全正确……“like”不存在,它应该存在比如,改为“创建”。