0

I'm honestly not even sure where to start with this problem but I've implemented a "like" feature on my site and when I'm looking at the console I see a REALLY REALLY long SQL statement.

I'm not going to post all of my dev.log because it's just INCREDIBLY long but here's a snippet of some of the errors..

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"]]

And then it just repeats that sql statement over ~10,000 lines and then it switches to this error

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'

I'm kind of stuck and I was hoping someone could point in in the right direction.

Here's my code:

Controller

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

Model:

class Like < ActiveRecord::Base
  belongs_to :chef
  belongs_to :recipe
end

I have the corresponding "has_many" in my other controllers"

Migration:

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

View:

<%= link_to like_recipe_path(@recipe, like: true), method: :post do %>
  <i class="glyphicon glyphicon-thumbs-up"></i>
<% end %>
Bryan Oemar
  • 916
  • 7
  • 16
Luke Xu
  • 2,302
  • 3
  • 19
  • 43
  • that's not seems to be enough data – Mani Nov 21 '15 at 10:02
  • `like.create(like: params[:like], chef: Chef.first, recipe: @recipe)` Where does this 'like' variable/method come from? It appears to be your Like model, but you're not setting it. So did you not post the whole code snippet perhaps? It might be useful to us if you can post more information. Could you do this? – Bryan Oemar Nov 21 '15 at 13:54
  • @BryanOemar HA! Good catch. You're absolutely right... "like" doesn't exist and it should be "Like.create" instead. – Luke Xu Nov 21 '15 at 16:00

1 Answers1

0

With the information above I think the only conclusion I can make is that the code redirect_to :back is calling an action which will call the action like again, which can result in a loop.

For example, if you're calling the 'like' action and that 'like' action is redirecting back to the 'like' action it will get stuck in a loop.

Try rendering the page if you want to stay on the same page or redirect_to :an_explicit_action where you set your variables and then render the page you want.

Bryan Oemar
  • 916
  • 7
  • 16