Devise showing user not signed in - ruby-on-rails

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.

Related

manage users by an admin in ruby on rails

how can I manage and edit other users profiles as an admin since I have one model and controller (users) ?
I tried to add a new action called updateusers
def updateusers
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
the problem here :it is updating my admin user with the other_user's data
stack trace
Started GET "/manage" for ::1 at 2016-03-19 21:06:08 +0300 Processing by UsersController#manage as HTML User Load (1.0ms) SELECT "users".* FROM "users" Rendered users/manage.html.erb within layouts/application (5.0ms) User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200 OK in 53ms (Views: 51.0ms | ActiveRecord: 1.0ms)
'Started GET "/users/10" for ::1 at 2016-03-19 21:06:10 +0300 Processing by UsersController#show as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered users/show.html.erb within layouts/application (0.0ms) User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200 OK in 37ms (Views: 36.0ms | ActiveRecord: 0.0ms)
Started GET "/editusers/10" for ::1 at 2016-03-19 21:06:11 +0300 Processing by UsersController#editusers as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered users/editusers.html.erb within layouts/application (4.0ms) User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200 OK in 41ms (Views: 39.0ms | ActiveRecord: 1.0ms)
Started PATCH "/users/10" for ::1 at 2016-03-19 21:06:15 +0300 Processing by UsersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"6M1TGLQUEhiezCCg9/rT5IofdroMiQ0sm+bYcihgGDxTjDdFGU2Riou2p‌​cRk5ncjCtFDGwfBj17Uq7gc0u329w==", "user"=>{"first_name"=>"g", "last_name"=>"g", "email"=>"g#g.g", "role"=>"editor", "image"=>"pic.png", "admins"=>""}, "other"=>"update", "id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Unpermitted parameters: role, admins
(0.0ms) begin transaction SQL (1.0ms) UPDATE "users" SET "first_name" = ?, "last_name" = ?, "email" = ?, "updated_at" = ? WHERE "users"."id" = ? [["first_name", "g"], ["last_name", "g"], ["email", "g#g.g"], ["updated_at", "2016-03-19 18:06:15.488284"], ["id", 1]] (47.0ms) commit transaction Redirected to localhost:8080/profile Completed 302 Found in 54ms (ActiveRecord: 48.0ms)
If it's updating the wrong user, it means that params[:id] is the id of the user being updated. Are you passing the id of the user you want to update in the params? Try calling puts params.inspect at the top of the controller action to see what data is being passed. You need to look up #other_user with their id and you need to make sure that #other_user's id is being passed with the other form data.
after 10 days ,, Yes i did it - the solution is in the name of submit , I named the two submits with diffrent names <%= f.submit "update", name:"other" %>
then i used the update action like this
def update
if params[:current]
#user = current_user
if #user.update_attributes(user_params)
redirect_to '/profile'
else
redirect_to '/edit'
end
elsif params[:other]
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
end

how to manage users by an admin in ruby on rails

how can I manage and edit other users profiles as an admin since I have one model and controller (users) ?
I tried to add a new action called updateusers
def updateusers
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
the problem here :it is updating my admin user with the other_user's
data
stack trace
Started GET "/manage" for ::1 at 2016-03-19 21:06:08 +0300 Processing by
UsersController#manage as HTML User Load (1.0ms) SELECT "users".* FROM
"users" Rendered users/manage.html.erb within layouts/application (5.0ms) User
Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1
[["id", 1]] Completed 200 OK in 53ms (Views: 51.0ms | ActiveRecord: 1.0ms)
'Started GET "/users/10" for ::1 at 2016-03-19 21:06:10 +0300 Processing by
UsersController#show as HTML Parameters: {"id"=>"10"} User Load (0.0ms) SELECT
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]] Rendered
users/show.html.erb within layouts/application (0.0ms) User Load (0.0ms) SELECT
"users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Completed 200
OK in 37ms (Views: 36.0ms | ActiveRecord: 0.0ms)
Started GET "/editusers/10" for ::1 at 2016-03-19 21:06:11 +0300 Processing
by UsersController#editusers as HTML Parameters: {"id"=>"10"} User Load (0.0ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
Rendered users/editusers.html.erb within layouts/application (4.0ms) User Load
(1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
Completed 200 OK in 41ms (Views: 39.0ms | ActiveRecord: 1.0ms)
Started PATCH "/users/10" for ::1 at 2016-03-19 21:06:15 +0300 Processing by
UsersController#update as HTML Parameters: {"utf8"=>"✓",
"authenticity_token"=>"6M1TGLQUEhiezCCg9/rT5IofdroMiQ0sm+bYcihgGDxTjDdFGU2Riou2p‌​
cRk5ncjCtFDGwfBj17Uq7gc0u329w==", "user"=>{"first_name"=>"g", "last_name"=>"g",
"email"=>"g#g.g", "role"=>"editor", "image"=>"pic.png", "admins"=>""},
"other"=>"update", "id"=>"10"} User Load (0.0ms) SELECT "users".* FROM "users"
WHERE "users"."id" = ? LIMIT 1 [["id", 1]] Unpermitted parameters: role, admins
(0.0ms) begin transaction SQL (1.0ms) UPDATE "users" SET "first_name" = ?,
"last_name" = ?, "email" = ?, "updated_at" = ? WHERE "users"."id" = ?
[["first_name", "g"], ["last_name", "g"], ["email", "g#g.g"], ["updated_at",
"2016-03-19 18:06:15.488284"], ["id", 1]] (47.0ms) commit transaction Redirected
to localhost:8080/profile Completed 302 Found in 54ms (ActiveRecord: 48.0ms)
The user ID of the form in "editusers" is set to your admin (or logged in user). It's hard to say without seeing the code but I think you've set up the editusers form incorrectly. Perhaps using a hidden field to hold the ID of the user you want to update.
Try to avoid that and set up the #user object in the 'editusers' action #user = User.find(10)
Then in your view use a form_for #user do |f| without any hidden fields for the ID.
after 10 days ,, Yes i did it - the solution is in the name of submit , I named the two submits with diffrent names <%= f.submit "update", name:"other" %> then i used the update action like this
def update
if params[:current]
#user = current_user
if #user.update_attributes(user_params)
redirect_to '/profile'
else
redirect_to '/edit'
end
elsif params[:other]
#other_user=User.find(params[:id])
if #other_user.update_attributes(otherusers_params)
redirect_to '/'
else
redirect_to '/manage'
end
end
end

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

Show attribute of form object via logger in rails

I want to debug my application and check what values are submitted on the form object in my controller class via logger ? How can it be done? Is there any better practice to debug rails application?
Your rails log (eg log/development.log) should already show everything that the form submitted, in the "Parameters". It will look like this:
Processing MusicServiceAdmin::UsersController#edit (for 127.0.0.1 at 2014-07-31 10:34:16) [GET]
Session ID: 3c0cfbe7ff23f8f718f6626748a4a
Parameters: {"id"=>"35363"}
Params
Each request in Rails is tracked using the console - you should examine the params which come through from there:
Started POST "/admin/login" for 127.0.0.1 at 2014-04-26 16:25:34 +0100
Processing by Admin::Users::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+I1TKfc1YDuil7EI1frrFIoerNg5uonB1CIiujJO0jo=", "user"=>{"email"=>"*********", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign In"}
[1m[36mUser Load (482.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`email` = 'rpeck#frontlineutilities.co.uk' ORDER BY `users`.`id` ASC LIMIT 1[0m
[1m[35m (435.0ms)[0m BEGIN
[1m[36mSQL (439.0ms)[0m [1mUPDATE `users` SET `current_sign_in_at` = '2014-04-26 15:25:35', `last_sign_in_at` = '2014-04-04 12:08:45', `sign_in_count` = 51, `updated_at` = '2014-04-26 15:25:36' WHERE `users`.`id` = 2[0m
[1m[35m (448.0ms)[0m COMMIT
[1m[36mOption Load (404.0ms)[0m [1mSELECT `options`.* FROM `options` WHERE `options`.`name` = 'site_robots' LIMIT 1[0m
[1m[35mOption Load (357.0ms)[0m SELECT `options`.* FROM `options` WHERE `options`.`name` = 'site_title' LIMIT 1
[1m[36mOption Load (428.0ms)[0m [1mSELECT `options`.* FROM `options` WHERE `options`.`name` = 'site_description' LIMIT 1[0m
Redirected to http://lvh.me:3000/****
Completed 302 Found in 3165ms (ActiveRecord: 2993.2ms)
If you want to "debug", you just have to examine the various params / actions which are rendered in the console. Alternatively, you may also wish to the use the Rails.logger.info method for specific data:
#app/controllers/your_controller.rb
Class YourController < ApplicationController
def index
Rails.logger.info("Test")
end
end

JS Request getting called twice in a row

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.

Resources