JS Request getting called twice in a row - ruby-on-rails

I'm working on the Ruby on Rails Tutorial. I have the asynchronous follow buttons working. Interestingly, each of the javascript calls is getting called twice in a row when I click the button. Any thoughts on how I make it only send one request? Here is my log
Started POST "/tag_user_relationships/123" for
127.0.0.1 at 2011-06-13 21:18:59 -0700 Processing by TagUserRelationshipsController#destroy as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"goedvibRxKtDRiAufp1ThWJP0rRBU2cMH2xp7qodKws=", "commit"=>"Unfollow", "id"=>"123"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 TagUserRelationship Load (0.2ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 LIMIT 1 Tag Load (0.2ms) SELECT `tags`.* FROM `tags` WHERE `tags`.`id`
= 9 LIMIT 1 TagUserRelationship Load (0.3ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`tag_id` = 9 AND (`tag_user_relationships`.user_id
= 2) LIMIT 1 SQL (0.1ms) BEGIN AREL (0.2ms) DELETE FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 SQL (0.4ms) COMMIT SQL (0.4ms) SELECT COUNT(*) FROM `users` INNER JOIN `tag_user_relationships` ON `users`.id = `tag_user_relationships`.user_id WHERE ((`tag_user_relationships`.tag_id = 9)) SQL (0.3ms) SELECT COUNT(*) FROM `tags` INNER JOIN `tag_user_relationships` ON `tags`.id
= `tag_user_relationships`.tag_id WHERE ((`tag_user_relationships`.user_id = 2)) Rendered tag_user_relationships/_form.js.erb (15.8ms) Rendered tags/_follow.html.erb (2.1ms) Rendered tag_user_relationships/destroy.js.erb (20.3ms) Completed 200 OK in 138ms (Views: 28.1ms | ActiveRecord: 2.3ms)
Started POST "/tag_user_relationships/123" for
127.0.0.1 at 2011-06-13 21:18:59 -0700 Processing by TagUserRelationshipsController#destroy as JS Parameters: {"utf8"=>"✓", "authenticity_token"=>"goedvibRxKtDRiAufp1ThWJP0rRBU2cMH2xp7qodKws=", "id"=>"123"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 TagUserRelationship Load (0.2ms) SELECT `tag_user_relationships`.* FROM `tag_user_relationships` WHERE `tag_user_relationships`.`id` = 123 LIMIT 1 Completed 404 Not Found in 70ms
ActiveRecord::RecordNotFound (Couldn't find TagUserRelationship with ID=123): app/controllers/tag_user_relationships_controller.rb:14:in `destroy'
Rendered /Users/me/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) Rendered /Users/me/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (153.6ms) Rendered /Users/me/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (160.2ms)
And here is my view
$("#<%= "follower_info#{#tag.id}" %>").html("<%=escape_javascript(pluralize(#tag.followers.count,'follower'))%>");
var link = $('<a>').attr('href',"<%=user_tags_path(current_user) %>").text("<%= escape_javascript(pluralize(current_user.beats.count,'tag')) %>");
$("#<%= "user#{current_user.id}_following" %>").html(link); $("#<%= "follow_form#{#tag.id}" %>").html("<%= escape_javascript("#{render('tags/unfollow', :tag => #tag)}").html_safe %>");
And my controller
def create
#tag = Tag.find(params[:tag_user_relationship][:tag_id])
current_user.follow_tag!(#tag)
respond_to do |format|
format.html { redirect_to #tag }
format.js
end
end

I had this same issue recently. Is there any chance that you installed the ajax gem and also included the jquery code in the Javascripts folder? If so, the calls will be made twice.

Related

Devise showing user not signed in

I'm using devise_token_auth and angular2-token. But Devise is not registering that a user is signed in.
I have a create definition that returns the current_user and the user_signed_in? status:
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :update, :destroy]
# POST /movies
def create
if Movie.exists?(title: movie_params[:title])
render json: { body: 'Movie already exists', status: 400 }
else
#user = current_user
#signed_in = user_signed_in?
render json: {
user: #user, status: #signed_in
}
end
end
def movie_params
# whitelist params
params.permit(:title, :created_by, :id)
end
end
When I run my application this returns user: null, status: false.
The applicationController:
class ApplicationController < ActionController::API
include DeviseTokenAuth::Concerns::SetUserByToken
end
The strange thing is that I do have a user signed in. This is what the Rails log shows when I reload the page:
Started GET "/auth/validate_token" for 127.0.0.1 at 2017-07-07 09:15:03 +0200
Started GET "/auth/validate_token" for 127.0.0.1 at 2017-07-07 09:15:03 +0200
Processing by DeviseTokenAuth::TokenValidationsController#validate_token as JSON
Processing by DeviseTokenAuth::TokenValidationsController#validate_token as JSON
Parameters: {"token_validation"=>{}}
Parameters: {"token_validation"=>{}}
User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`uid` = 'peter#cleanpixel.nl' LIMIT 1
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`uid` = 'peter#cleanpixel.nl' LIMIT 1
(1.5ms) BEGIN
(0.0ms) BEGIN
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 FOR UPDATE
SQL (0.5ms) UPDATE `users` SET `tokens` = '{\"dj9nsHh0XTBTnxaBT3kyiw\":{\"token\":\"$2a$10$otdEoY.3ATpQtcMPvOIxkeJKjcn4hHG.OhOAa5hN/haKjoD8mdEHK\",\"expiry\":1500566765,\"last_token\":\"$2a$10$gWHnaLi.MQCrSpc3uqZI.uR6dsbSv3.OAbA/VmFQFOSwYy.ilzbSy\",\"updated_at\":\"2017-07-06T18:06:05.349+02:00\"},\"Zb9n6Xsuv8uDoz0UgREIvA\":{\"token\":\"$2a$10$WsH4tTHx3t7olJwdNNekG.zxCo2.5CRPYvTJQN1muKZnBeKD3z8Eq\",\"expiry\":1500569836,\"last_token\":\"$2a$10$cK1y.pzug.PpTi53/bxMG.A.h8idbngy1G4G07fke1YKCCBqmxBXK\",\"updated_at\":\"2017-07-06T18:57:16.971+02:00\"},\"cqBp_VyXAgnwQaboLddNqA\":{\"token\":\"$2a$10$aHpZo34C3BOgBCRuMZXA8u8Dm9eQbaZBncMfAUExcJnSzBgXoHWta\",\"expiry\":1500567653,\"last_token\":\"$2a$10$N9la9pzAkmJotIf/nVmx6uOmHVBhYjagMTKhCb/52hVFhlwhbotz2\",\"updated_at\":\"2017-07-06T18:20:55.382+02:00\"},\"-1YucaseQui9xTM8joV1bA\":{\"token\":\"$2a$10$ZTYVkIcPjl7VDi45RkkB3uvZLM8I5GYgNnm1Go602T4AUP40TWmIK\",\"expiry\":1500570282,\"last_token\":\"$2a$10$Blv8cg7Q0Nq2l9mYoWpDe./W4Bmqw6Tadvh0rb63PQTJ9O8BbWBD.\",\"updated_at\":\"2017-07-06T19:04:42.416+02:00\"},\"r37dOyCTPEPPF0E3n4MJAw\":{\"token\":\"$2a$10$gb8ERl211K2HcHe6X1SX1ex2yQ4L9JqJQF5dLM.faEUeIgW0v1Sma\",\"expiry\":1500573270,\"last_token\":\"$2a$10$ol47NlxNVlWTOc6LuOQHNuaFJOpxO/I8huGuB/1wwiFT3XjCNFKPW\",\"updated_at\":\"2017-07-06T19:54:30.343+02:00\"},\"7FWoXqZgN7WDmkOIgonMDA\":{\"token\":\"$2a$10$K4gLINjrtFvEbmGG9wM88OfMbJF3y4mw39U8Y/io9awRTBtLNhyTa\",\"expiry\":1500573600,\"last_token\":\"$2a$10$gucXe2txI3XT5bCfqP5QfegS.c.DAJLAzemgxm1YSbyGXutt01b02\",\"updated_at\":\"2017-07-06T20:00:01.178+02:00\"},\"pUk1lCvCOmCyxfFKv4Tmnw\":{\"token\":\"$2a$10$U7RL54MKDFPQCq/R7uhjDeafKtqpj9X1zCAk1kWmIjYkWUeaCpfcS\",\"expiry\":1500574025,\"last_token\":\"$2a$10$p0Unq1sHVowseikwok7HqOVw/wAry/K1RZHUrzL0.OMdVQ1T.cYPS\",\"updated_at\":\"2017-07-06T20:07:05.733+02:00\"},\"Zpq95Nuc5UOP6PEK4nDCjQ\":{\"token\":\"$2a$10$C5f/nxd35s/6HmewsWmMU.aB4GHn7Hh6NTJEWR3xxzAwA4iHKDZlC\",\"expiry\":1500575385,\"last_token\":\"$2a$10$yjQGp/F.dHBipka.DHheaeSHhMDyPVukLhxhiE4V79BsJrytIDfOi\",\"updated_at\":\"2017-07-06T20:29:45.673+02:00\"},\"2vGGRC7gOCFTBiYSkMCfXQ\":{\"token\":\"$2a$10$ykxrUsRX0yZOtLW03cbqyezhVQ4fqnLHBrVIgilIaxhp0d8hqCE0m\",\"expiry\":1500618218,\"last_token\":\"$2a$10$dEf3s9O.Upz4cG0xAwEOC.Axlw70Xm9fsy9f2UP5iqffGT1BPLEze\",\"updated_at\":\"2017-07-07T08:23:38.253+02:00\"},\"kkuI52Vq05TOCSVBgHGWHA\":{\"token\":\"$2a$10$XDPo.nKeRj.blboiVey8u.sh7vlubmRo5k/B86l5AvL3ZJw.0yjhG\",\"expiry\":1500621303,\"last_token\":\"$2a$10$RkvthXkouoosD2l3OVFNqetvlfG686Ltac1TAlO9to0SMOpSgpKue\",\"updated_at\":\"2017-07-07T09:15:03.317+02:00\"}}', `updated_at` = '2017-07-07 07:15:03' WHERE `users`.`id` = 2
User Load (67.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 FOR UPDATE
(3.5ms) COMMIT
Completed 200 OK in 137ms (Views: 0.2ms | ActiveRecord: 6.5ms)
SQL (0.5ms) UPDATE `users` SET `tokens` = '{\"dj9nsHh0XTBTnxaBT3kyiw\":{\"token\":\"$2a$10$otdEoY.3ATpQtcMPvOIxkeJKjcn4hHG.OhOAa5hN/haKjoD8mdEHK\",\"expiry\":1500566765,\"last_token\":\"$2a$10$gWHnaLi.MQCrSpc3uqZI.uR6dsbSv3.OAbA/VmFQFOSwYy.ilzbSy\",\"updated_at\":\"2017-07-06T18:06:05.349+02:00\"},\"Zb9n6Xsuv8uDoz0UgREIvA\":{\"token\":\"$2a$10$WsH4tTHx3t7olJwdNNekG.zxCo2.5CRPYvTJQN1muKZnBeKD3z8Eq\",\"expiry\":1500569836,\"last_token\":\"$2a$10$cK1y.pzug.PpTi53/bxMG.A.h8idbngy1G4G07fke1YKCCBqmxBXK\",\"updated_at\":\"2017-07-06T18:57:16.971+02:00\"},\"cqBp_VyXAgnwQaboLddNqA\":{\"token\":\"$2a$10$aHpZo34C3BOgBCRuMZXA8u8Dm9eQbaZBncMfAUExcJnSzBgXoHWta\",\"expiry\":1500567653,\"last_token\":\"$2a$10$N9la9pzAkmJotIf/nVmx6uOmHVBhYjagMTKhCb/52hVFhlwhbotz2\",\"updated_at\":\"2017-07-06T18:20:55.382+02:00\"},\"-1YucaseQui9xTM8joV1bA\":{\"token\":\"$2a$10$ZTYVkIcPjl7VDi45RkkB3uvZLM8I5GYgNnm1Go602T4AUP40TWmIK\",\"expiry\":1500570282,\"last_token\":\"$2a$10$Blv8cg7Q0Nq2l9mYoWpDe./W4Bmqw6Tadvh0rb63PQTJ9O8BbWBD.\",\"updated_at\":\"2017-07-06T19:04:42.416+02:00\"},\"r37dOyCTPEPPF0E3n4MJAw\":{\"token\":\"$2a$10$gb8ERl211K2HcHe6X1SX1ex2yQ4L9JqJQF5dLM.faEUeIgW0v1Sma\",\"expiry\":1500573270,\"last_token\":\"$2a$10$ol47NlxNVlWTOc6LuOQHNuaFJOpxO/I8huGuB/1wwiFT3XjCNFKPW\",\"updated_at\":\"2017-07-06T19:54:30.343+02:00\"},\"7FWoXqZgN7WDmkOIgonMDA\":{\"token\":\"$2a$10$K4gLINjrtFvEbmGG9wM88OfMbJF3y4mw39U8Y/io9awRTBtLNhyTa\",\"expiry\":1500573600,\"last_token\":\"$2a$10$gucXe2txI3XT5bCfqP5QfegS.c.DAJLAzemgxm1YSbyGXutt01b02\",\"updated_at\":\"2017-07-06T20:00:01.178+02:00\"},\"pUk1lCvCOmCyxfFKv4Tmnw\":{\"token\":\"$2a$10$U7RL54MKDFPQCq/R7uhjDeafKtqpj9X1zCAk1kWmIjYkWUeaCpfcS\",\"expiry\":1500574025,\"last_token\":\"$2a$10$p0Unq1sHVowseikwok7HqOVw/wAry/K1RZHUrzL0.OMdVQ1T.cYPS\",\"updated_at\":\"2017-07-06T20:07:05.733+02:00\"},\"Zpq95Nuc5UOP6PEK4nDCjQ\":{\"token\":\"$2a$10$C5f/nxd35s/6HmewsWmMU.aB4GHn7Hh6NTJEWR3xxzAwA4iHKDZlC\",\"expiry\":1500575385,\"last_token\":\"$2a$10$yjQGp/F.dHBipka.DHheaeSHhMDyPVukLhxhiE4V79BsJrytIDfOi\",\"updated_at\":\"2017-07-06T20:29:45.673+02:00\"},\"2vGGRC7gOCFTBiYSkMCfXQ\":{\"token\":\"$2a$10$ykxrUsRX0yZOtLW03cbqyezhVQ4fqnLHBrVIgilIaxhp0d8hqCE0m\",\"expiry\":1500618218,\"last_token\":\"$2a$10$dEf3s9O.Upz4cG0xAwEOC.Axlw70Xm9fsy9f2UP5iqffGT1BPLEze\",\"updated_at\":\"2017-07-07T08:23:38.253+02:00\"},\"kkuI52Vq05TOCSVBgHGWHA\":{\"token\":\"$2a$10$XDPo.nKeRj.blboiVey8u.sh7vlubmRo5k/B86l5AvL3ZJw.0yjhG\",\"expiry\":1500621303,\"last_token\":\"$2a$10$RkvthXkouoosD2l3OVFNqetvlfG686Ltac1TAlO9to0SMOpSgpKue\",\"updated_at\":\"2017-07-07T09:15:03.323+02:00\"}}' WHERE `users`.`id` = 2
(2.5ms) COMMIT
Completed 200 OK in 142ms (Views: 0.1ms | ActiveRecord: 71.1ms)
And this is what Rails logs when I log in my user:
Started OPTIONS "/auth/sign_in" for 127.0.0.1 at 2017-07-07 09:22:44 +0200
Started POST "/auth/sign_in" for 127.0.0.1 at 2017-07-07 09:22:44 +0200
Processing by DeviseTokenAuth::SessionsController#create as JSON
Parameters: {"email"=>"peter#cleanpixel.nl", "password"=>"[FILTERED]", "session"=>{"email"=>"peter#cleanpixel.nl", "password"=>"[FILTERED]"}}
Unpermitted parameter: :session
Unpermitted parameter: :session
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE (BINARY email = 'peter#cleanpixel.nl' AND provider='email') ORDER BY `users`.`id` ASC LIMIT 1
Unpermitted parameter: :session
Unpermitted parameter: :session
(0.5ms) BEGIN
SQL (1.0ms) UPDATE `users` SET `tokens` = '{\"dj9nsHh0XTBTnxaBT3kyiw\":{\"token\":\"$2a$10$otdEoY.3ATpQtcMPvOIxkeJKjcn4hHG.OhOAa5hN/haKjoD8mdEHK\",\"expiry\":1500566765,\"last_token\":\"$2a$10$gWHnaLi.MQCrSpc3uqZI.uR6dsbSv3.OAbA/VmFQFOSwYy.ilzbSy\",\"updated_at\":\"2017-07-06T18:06:05.349+02:00\"},\"Zb9n6Xsuv8uDoz0UgREIvA\":{\"token\":\"$2a$10$WsH4tTHx3t7olJwdNNekG.zxCo2.5CRPYvTJQN1muKZnBeKD3z8Eq\",\"expiry\":1500569836,\"last_token\":\"$2a$10$cK1y.pzug.PpTi53/bxMG.A.h8idbngy1G4G07fke1YKCCBqmxBXK\",\"updated_at\":\"2017-07-06T18:57:16.971+02:00\"},\"cqBp_VyXAgnwQaboLddNqA\":{\"token\":\"$2a$10$aHpZo34C3BOgBCRuMZXA8u8Dm9eQbaZBncMfAUExcJnSzBgXoHWta\",\"expiry\":1500567653,\"last_token\":\"$2a$10$N9la9pzAkmJotIf/nVmx6uOmHVBhYjagMTKhCb/52hVFhlwhbotz2\",\"updated_at\":\"2017-07-06T18:20:55.382+02:00\"},\"-1YucaseQui9xTM8joV1bA\":{\"token\":\"$2a$10$ZTYVkIcPjl7VDi45RkkB3uvZLM8I5GYgNnm1Go602T4AUP40TWmIK\",\"expiry\":1500570282,\"last_token\":\"$2a$10$Blv8cg7Q0Nq2l9mYoWpDe./W4Bmqw6Tadvh0rb63PQTJ9O8BbWBD.\",\"updated_at\":\"2017-07-06T19:04:42.416+02:00\"},\"r37dOyCTPEPPF0E3n4MJAw\":{\"token\":\"$2a$10$gb8ERl211K2HcHe6X1SX1ex2yQ4L9JqJQF5dLM.faEUeIgW0v1Sma\",\"expiry\":1500573270,\"last_token\":\"$2a$10$ol47NlxNVlWTOc6LuOQHNuaFJOpxO/I8huGuB/1wwiFT3XjCNFKPW\",\"updated_at\":\"2017-07-06T19:54:30.343+02:00\"},\"7FWoXqZgN7WDmkOIgonMDA\":{\"token\":\"$2a$10$K4gLINjrtFvEbmGG9wM88OfMbJF3y4mw39U8Y/io9awRTBtLNhyTa\",\"expiry\":1500573600,\"last_token\":\"$2a$10$gucXe2txI3XT5bCfqP5QfegS.c.DAJLAzemgxm1YSbyGXutt01b02\",\"updated_at\":\"2017-07-06T20:00:01.178+02:00\"},\"pUk1lCvCOmCyxfFKv4Tmnw\":{\"token\":\"$2a$10$U7RL54MKDFPQCq/R7uhjDeafKtqpj9X1zCAk1kWmIjYkWUeaCpfcS\",\"expiry\":1500574025,\"last_token\":\"$2a$10$p0Unq1sHVowseikwok7HqOVw/wAry/K1RZHUrzL0.OMdVQ1T.cYPS\",\"updated_at\":\"2017-07-06T20:07:05.733+02:00\"},\"Zpq95Nuc5UOP6PEK4nDCjQ\":{\"token\":\"$2a$10$C5f/nxd35s/6HmewsWmMU.aB4GHn7Hh6NTJEWR3xxzAwA4iHKDZlC\",\"expiry\":1500575385,\"last_token\":\"$2a$10$yjQGp/F.dHBipka.DHheaeSHhMDyPVukLhxhiE4V79BsJrytIDfOi\",\"updated_at\":\"2017-07-06T20:29:45.673+02:00\"},\"2vGGRC7gOCFTBiYSkMCfXQ\":{\"token\":\"$2a$10$ykxrUsRX0yZOtLW03cbqyezhVQ4fqnLHBrVIgilIaxhp0d8hqCE0m\",\"expiry\":1500618218,\"last_token\":\"$2a$10$dEf3s9O.Upz4cG0xAwEOC.Axlw70Xm9fsy9f2UP5iqffGT1BPLEze\",\"updated_at\":\"2017-07-07T08:23:38.253+02:00\"},\"kkuI52Vq05TOCSVBgHGWHA\":{\"token\":\"$2a$10$XDPo.nKeRj.blboiVey8u.sh7vlubmRo5k/B86l5AvL3ZJw.0yjhG\",\"expiry\":1500621303,\"last_token\":\"$2a$10$RkvthXkouoosD2l3OVFNqetvlfG686Ltac1TAlO9to0SMOpSgpKue\",\"updated_at\":\"2017-07-07T09:15:03.323+02:00\"},\"DqpFWUYwTyHsq0z5V-H3sA\":{\"token\":\"$2a$10$.Ssm5ZSlVL/4Wr3FOUkOTOs/xP/V/vxGS01.zwe.5ictpoOD7xsHi\",\"expiry\":1500621764}}', `updated_at` = '2017-07-07 07:22:44' WHERE `users`.`id` = 2
(1.0ms) COMMIT
(0.0ms) BEGIN
SQL (0.5ms) UPDATE `users` SET `sign_in_count` = 23, `current_sign_in_at` = '2017-07-07 07:22:44', `last_sign_in_at` = '2017-07-07 07:00:58' WHERE `users`.`id` = 2
(2.0ms) COMMIT
(0.0ms) BEGIN
User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 FOR UPDATE
SQL (0.5ms) UPDATE `users` SET `tokens` = '{\"Zb9n6Xsuv8uDoz0UgREIvA\":{\"token\":\"$2a$10$WsH4tTHx3t7olJwdNNekG.zxCo2.5CRPYvTJQN1muKZnBeKD3z8Eq\",\"expiry\":1500569836,\"last_token\":\"$2a$10$cK1y.pzug.PpTi53/bxMG.A.h8idbngy1G4G07fke1YKCCBqmxBXK\",\"updated_at\":\"2017-07-06T18:57:16.971+02:00\"},\"cqBp_VyXAgnwQaboLddNqA\":{\"token\":\"$2a$10$aHpZo34C3BOgBCRuMZXA8u8Dm9eQbaZBncMfAUExcJnSzBgXoHWta\",\"expiry\":1500567653,\"last_token\":\"$2a$10$N9la9pzAkmJotIf/nVmx6uOmHVBhYjagMTKhCb/52hVFhlwhbotz2\",\"updated_at\":\"2017-07-06T18:20:55.382+02:00\"},\"-1YucaseQui9xTM8joV1bA\":{\"token\":\"$2a$10$ZTYVkIcPjl7VDi45RkkB3uvZLM8I5GYgNnm1Go602T4AUP40TWmIK\",\"expiry\":1500570282,\"last_token\":\"$2a$10$Blv8cg7Q0Nq2l9mYoWpDe./W4Bmqw6Tadvh0rb63PQTJ9O8BbWBD.\",\"updated_at\":\"2017-07-06T19:04:42.416+02:00\"},\"r37dOyCTPEPPF0E3n4MJAw\":{\"token\":\"$2a$10$gb8ERl211K2HcHe6X1SX1ex2yQ4L9JqJQF5dLM.faEUeIgW0v1Sma\",\"expiry\":1500573270,\"last_token\":\"$2a$10$ol47NlxNVlWTOc6LuOQHNuaFJOpxO/I8huGuB/1wwiFT3XjCNFKPW\",\"updated_at\":\"2017-07-06T19:54:30.343+02:00\"},\"7FWoXqZgN7WDmkOIgonMDA\":{\"token\":\"$2a$10$K4gLINjrtFvEbmGG9wM88OfMbJF3y4mw39U8Y/io9awRTBtLNhyTa\",\"expiry\":1500573600,\"last_token\":\"$2a$10$gucXe2txI3XT5bCfqP5QfegS.c.DAJLAzemgxm1YSbyGXutt01b02\",\"updated_at\":\"2017-07-06T20:00:01.178+02:00\"},\"pUk1lCvCOmCyxfFKv4Tmnw\":{\"token\":\"$2a$10$U7RL54MKDFPQCq/R7uhjDeafKtqpj9X1zCAk1kWmIjYkWUeaCpfcS\",\"expiry\":1500574025,\"last_token\":\"$2a$10$p0Unq1sHVowseikwok7HqOVw/wAry/K1RZHUrzL0.OMdVQ1T.cYPS\",\"updated_at\":\"2017-07-06T20:07:05.733+02:00\"},\"Zpq95Nuc5UOP6PEK4nDCjQ\":{\"token\":\"$2a$10$C5f/nxd35s/6HmewsWmMU.aB4GHn7Hh6NTJEWR3xxzAwA4iHKDZlC\",\"expiry\":1500575385,\"last_token\":\"$2a$10$yjQGp/F.dHBipka.DHheaeSHhMDyPVukLhxhiE4V79BsJrytIDfOi\",\"updated_at\":\"2017-07-06T20:29:45.673+02:00\"},\"2vGGRC7gOCFTBiYSkMCfXQ\":{\"token\":\"$2a$10$ykxrUsRX0yZOtLW03cbqyezhVQ4fqnLHBrVIgilIaxhp0d8hqCE0m\",\"expiry\":1500618218,\"last_token\":\"$2a$10$dEf3s9O.Upz4cG0xAwEOC.Axlw70Xm9fsy9f2UP5iqffGT1BPLEze\",\"updated_at\":\"2017-07-07T08:23:38.253+02:00\"},\"kkuI52Vq05TOCSVBgHGWHA\":{\"token\":\"$2a$10$XDPo.nKeRj.blboiVey8u.sh7vlubmRo5k/B86l5AvL3ZJw.0yjhG\",\"expiry\":1500621303,\"last_token\":\"$2a$10$RkvthXkouoosD2l3OVFNqetvlfG686Ltac1TAlO9to0SMOpSgpKue\",\"updated_at\":\"2017-07-07T09:15:03.323+02:00\"},\"DqpFWUYwTyHsq0z5V-H3sA\":{\"token\":\"$2a$10$vaeWtFcZiwA85dtRtwhRj.Ko0WMS.9gL0eDdhfTVoIIvIT7aN8BEy\",\"expiry\":1500621764,\"last_token\":\"$2a$10$.Ssm5ZSlVL/4Wr3FOUkOTOs/xP/V/vxGS01.zwe.5ictpoOD7xsHi\",\"updated_at\":\"2017-07-07T09:22:44.824+02:00\"}}' WHERE `users`.`id` = 2
(1.5ms) COMMIT
Completed 200 OK in 235ms (Views: 0.2ms | ActiveRecord: 9.0ms)
So to me it looks like the user is sign in the application, but devise is not returning anything.
//EDIT//
I've cloned https://github.com/neroniaky/angular2-token-example and that's working locally. Although I still get the 'Unpermitted parameter: session' error. It looks like there's something wrong with my front-end though. Going to investigate and post the result later.

How do I perform a where query on an enum attribute with a string rather than the ordinal value?

So I have a model that has an enum value like so:
class Connection < ActiveRecord::Base
enum request_status: { pending: 0, accepted: 1, rejected: 2, removed: 3 }
end
But, I have a params value that I want to set and do a where query on - something like this:
#connections = current_user.all_connections.where(request_status: params[:request_status])
current_user.all_connections returns an AR object (aka...it is also a query method with a where call).
The issue I am facing is that right now, the above query returns an empty collection. The reason I believe is that per the API docs in Rails:
Where conditions on an enum attribute must use the ordinal value of an
enum.
Given that params[:request_status] will look like this:
Parameters: {"request_status"=>"accepted"}
How would I modify that where query to reflect this? I know one other option is just to modify the parameter to be the ordinal value, rather than the string, but I am curious how I might achieve this with the string value rather than the ordinal value?
Edit 1
Here are the server logs:
Started GET "/connections?request_status=accepted" for 127.0.0.1 at 2016-01-04 20:23:08 -0500
Processing by ConnectionsController#index as HTML
Parameters: {"request_status"=>"accepted"}
User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (1.7ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 3]]
Connection Load (2.0ms) SELECT "connections".* FROM "connections" WHERE "connections"."invited_user_id" = $1 ORDER BY "connections"."created_at" DESC [["invited_user_id", 3]]
Connection Load (1.8ms) SELECT "connections".* FROM "connections" WHERE "connections"."inviter_user_id" = $1 ORDER BY "connections"."created_at" DESC [["inviter_user_id", 3]]
Connection Load (2.2ms) SELECT "connections".* FROM "connections" WHERE (connections.invited_user_id = 3 OR connections.inviter_user_id = 3) AND "connections"."request_status" = 0 ORDER BY "connections"."created_at" DESC
Rendered connections/index.html.erb within layouts/connections (0.3ms)
Rendered connections/_header.html.erb (2.8ms)
Rendered shared/_navbar.html.erb (61.2ms)
Rendered shared/_footer.html.erb (3.2ms)
Rendered layouts/application.html.erb (1142.4ms)
Completed 200 OK in 1184ms (Views: 1155.5ms | ActiveRecord: 10.4ms)
Edit 2
This is the index action for that controller.
def index
params[:request_status] = "pending" if params[:request_status].nil?
#connections = current_user.all_connections.where(request_status: params[:request_status]).order(created_at: :desc).group_by { |c| c.created_at.to_date }
end
What's strange is that for every action that I click on that should send the correct request_status it still sends the SQL with request_status = 0.
I commented out the params[:request_status] = setter in the controller to see if that was doing it, but it wasn't it.
What could be causing this issue?
Here is another log for a different status:
Started GET "/connections?request_status=rejected" for 127.0.0.1 at 2016-01-04 21:30:29 -0500
Processing by ConnectionsController#index as HTML
Parameters: {"request_status"=>"rejected"}
User Load (2.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (1.5ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 3]]
Connection Load (1.4ms) SELECT "connections".* FROM "connections" WHERE "connections"."invited_user_id" = $1 ORDER BY "connections"."created_at" DESC [["invited_user_id", 3]]
Connection Load (1.0ms) SELECT "connections".* FROM "connections" WHERE "connections"."inviter_user_id" = $1 ORDER BY "connections"."created_at" DESC [["inviter_user_id", 3]]
Connection Load (1.2ms) SELECT "connections".* FROM "connections" WHERE (connections.invited_user_id = 3 OR connections.inviter_user_id = 3) AND "connections"."request_status" = 0 ORDER BY "connections"."created_at" DESC
Rendered connections/index.html.erb within layouts/connections (0.2ms)
Rendered connections/_header.html.erb (2.5ms)
Rendered shared/_navbar.html.erb (60.3ms)
Rendered shared/_footer.html.erb (3.0ms)
Rendered layouts/application.html.erb (1103.8ms)
Completed 200 OK in 1130ms (Views: 1112.5ms | ActiveRecord: 7.3ms)
You can use Connection.request_statuses["pending"] to get 0.
So you can use string in your query like this:
#connections = current_user.all_connections.where(request_status: Connection.request_statuses[params[:request_status]])

How to order a collection of a habtm-association by created_at

I have three main Classes: Users, Vocabs and Tags. A User has many Tags. Tags has-and-belongs-to-many Vocabs (and the other way around).
How can I order a collection of Tags when getting them through a Vocab-Object?
#vocab.tag
gets the tags from the joins table, which has no created_at column.
Is there a handy way to solve this problem?
I am pretty new to Rails so excuse me if this is obvious.
EDIT: I just tried
#tags = #vocab.tags.order('tags.created_at DESC')
but without success.
The computer says:
Started GET "/users/1/vocabs/61" for 127.0.0.1 at 2014-12-11 15:41:15 +0100
Processing by VocabsController#show as HTML
Parameters: {"user_id"=>"1", "id"=>"61"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Vocab Load (0.3ms) SELECT "vocabs".* FROM "vocabs" WHERE "vocabs"."user_id" = ? AND "vocabs"."id"
= ? ORDER BY created_at DESC LIMIT 1 [["user_id", 1], ["id", 61]]
Tag Load (0.5ms) SELECT DISTINCT "tags".* FROM "tags" INNER JOIN "tags_vocabs" ON "tags"."id" =
"tags_vocabs"."tag_id" WHERE "tags_vocabs"."vocab_id" = ? [["vocab_id", 61]]
Rendered shared/_error_messages.html.erb (0.2ms)
Rendered vocabs/show.html.erb within layouts/application (10.7ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (1.1ms)
Completed 200 OK in 602ms (Views: 587.3ms | ActiveRecord: 1.2ms)
The controller action looks like this:
def new_tag
#user = current_user
#vocab = #user.vocabs.find(params[:id])
#tags = #vocab.tags.order('tags.created_at DESC')
#tag = current_user.tags.build(name: params[:tag])
if #tag.save
#vocab.tags<<#tag
flash.now[:success] ='Tag successfully created.'
redirect_to user_vocab_path(#user, #vocab)
else
flash.now[:danger] = "Tag could not be created!"
render :action => "show"
end
end

Seems like Rails do caching, which I don't want it to

There is #avalibable_colors=['#5A009D', '#004DFF', '#F4F400', '#FF8000'] variable (it is not instance of model), which represent avaliable options - colors, which hadn't been taken yet, through method:
def choose_color
#check=Check.find(params[:check_id])
#colorschemes=#check.colorschemes
current_colors=[]
#check.colorschemes.each do |c|
current_colors.push(c.color)
end
#avalibable_colors=['#5A009D', '#004DFF', '#F4F400', '#FF8000']
current_colors.each do |c|
if #avalibable_colors.index(c)
#avalibable_colors[#avalibable_colors.index(c)]=nil
end
end
#avalibable_colors.compact!
respond_to do |format|
format.js
end
end
After that #avalibable_colors contents only untaken color options.
And generally it works. If I choose 1 color option, it displays rest 4, if 2 - rest 3, if I choose one, and then delete it - it is in avaliable colors.
And that works until whole four. And If I add all four, and then delete one by one, no colors are avaliable until I refresh page.
Server logs:
Started GET "/choose_color?check_id=46" for 127.0.0.1 at 2013-06-13 19:00:31 +0400
Processing by ColorschemesController#choose_color as JS
Parameters: {"check_id"=>"46"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'oPXJtztDkYdYmVsQ3wxxfQ' LIMIT 1
Check Load (1.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = 46 LIMIT 1
CACHE (0.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = 46 LIMIT 1
Check Load (1.0ms) SELECT "checks".* FROM "checks" WHERE "checks"."id" = $1 LIMIT 1 [["id", "46"]]
Colorscheme Load (0.0ms) SELECT "colorschemes".* FROM "colorschemes" WHERE "colorschemes"."check_id" = 46
Rendered colorschemes/_choose_color.html.erb (0.0ms)
Rendered colorschemes/choose_color.js.erb (5.0ms)
Completed 200 OK in 33ms (Views: 22.0ms | ActiveRecord: 3.0ms)

Rails, dirty objects causing rollbacks

I have the following:
class User < ActiveRecord::Base
before_update :guest_upgrade
def guest_upgrade
# If the user changed their email that means they were a guest, and are no longer.
# Likely triggered from the Registrations#Update controller
if self.email_changed?
self.guest = false
end
end
This is causing rollbacks, here is the log with the above in play:
Started POST "/users" for 127.0.0.1 at 2011-07-18 14:54:00 -0700
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"x+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"xxxxx", "lname"=>"xxxx", "email"=>"xxxxxxx#gmail.com", "password"=>"[FILTERED]", "remember_me"=>"1"}, "commit"=>"Create my account", "fb_access_token"=>"", "fb_uuid"=>""}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
SQL (0.1ms) BEGIN
User Load (0.5ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxxxxx#gmail.com') AND ("users".id <> 5) LIMIT 1
SQL (0.2ms) ROLLBACK
Authentication Load (0.5ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered layouts/_header.html.erb (3.9ms)
CACHE (0.0ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND ("authentications".user_id = 5) LIMIT 1
Rendered registrations/edit.html.erb within layouts/application (107.0ms)
Completed 200 OK in 484ms (Views: 111.7ms | ActiveRecord: 6.0ms)
Yet if I comment out the guest_upgrade it works fine:
Started POST "/users" for 127.0.0.1 at 2011-07-18 14:55:23 -0700
Processing by RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxx+u1DSDanU2QXK/q0=", "user"=>{"fname"=>"XXXX", "lname"=>"XXXX", "email"=>"xxxx#gmail.com"}, "commit"=>"Save Changes"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
SQL (0.1ms) BEGIN
User Load (0.4ms) SELECT "users"."id" FROM "users" WHERE ("users"."email" = 'xxxx#gmail.com') AND ("users".id <> 5) LIMIT 1
AREL (0.7ms) UPDATE "users" SET "fname" = 'XXXX', "lname" = 'XXXX', "email" = 'xxxx#gmail.com', "updated_at" = '2011-07-18 21:55:23.817142' WHERE "users"."id" = 5
[paperclip] Saving attachments.
SQL (37.9ms) COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 780ms
Am I using the dirty objects incorrectly?
All I want to do is, when User is updated, if the user changes there email, change the user.guest field to false.
Suggestions? Thanks
I think I've had this problem before. I believe what's happening is it's returning false; try adding an explicit return like so:
if self.email_changed?
self.guest = false
return true
end
Check out the section marked Canceling Callbacks at ruby on rails.org api. It reads:
Canceling callbacks
If a before_* callback returns false, all the later callbacks and the
associated action are cancelled. If an after_* callback returns false,
all the later callbacks are cancelled. Callbacks are generally run in
the order they are defined, with the exception of callbacks defined as
methods on the model, which are called last.
I know I'm late but I hope this helps!

Resources