how can i show user object data by passing user authentication_token in rails 5 restful json api?
in v1/controller
class V1::ProfilesController < ApplicationController
def index
if user = User.authentication_keys.present?
user = User.all
render json: {status: 'load', message:'load', user: 'user'},status: :ok
else
render json: {status: 'error', message:'error', user: 'user'},status: :ok
end
end
in routes
namespace :v1 do
resources :profiles
end
i am checking data in postman by passing authentication_token but it not show user object postman image]1
my user data in rails console
User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"
ruby ruby-on-rails-5
add a comment |
in v1/controller
class V1::ProfilesController < ApplicationController
def index
if user = User.authentication_keys.present?
user = User.all
render json: {status: 'load', message:'load', user: 'user'},status: :ok
else
render json: {status: 'error', message:'error', user: 'user'},status: :ok
end
end
in routes
namespace :v1 do
resources :profiles
end
i am checking data in postman by passing authentication_token but it not show user object postman image]1
my user data in rails console
User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"
ruby ruby-on-rails-5
i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:24
add a comment |
in v1/controller
class V1::ProfilesController < ApplicationController
def index
if user = User.authentication_keys.present?
user = User.all
render json: {status: 'load', message:'load', user: 'user'},status: :ok
else
render json: {status: 'error', message:'error', user: 'user'},status: :ok
end
end
in routes
namespace :v1 do
resources :profiles
end
i am checking data in postman by passing authentication_token but it not show user object postman image]1
my user data in rails console
User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"
ruby ruby-on-rails-5
in v1/controller
class V1::ProfilesController < ApplicationController
def index
if user = User.authentication_keys.present?
user = User.all
render json: {status: 'load', message:'load', user: 'user'},status: :ok
else
render json: {status: 'error', message:'error', user: 'user'},status: :ok
end
end
in routes
namespace :v1 do
resources :profiles
end
i am checking data in postman by passing authentication_token but it not show user object postman image]1
my user data in rails console
User id: 1, email: "adarsh1454@codekyt.com", created_at: "2018-11-14 07:35:59", updated_at: "2018-11-14 07:35:59", authentication_token: "tYHzjLm-6xxCeM4RXyEe"
ruby ruby-on-rails-5
ruby ruby-on-rails-5
edited Nov 14 '18 at 9:43
adarsh
asked Nov 14 '18 at 8:48
adarshadarsh
12411
12411
i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:24
add a comment |
i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:24
i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:24
i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:24
add a comment |
2 Answers
2
active
oldest
votes
You are passing 'user'
as a string to your json. So it will just send that string.
You could try doing something like:
render json: {status: 'load', message:'load', user: user.to_json},status: :ok
Or even:
render json: user
That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.
edit
OK, from your comment I think you probably need something along the lines of:
def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end
users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help
– adarsh
Nov 14 '18 at 12:20
add a comment |
In successful case, you respond with the following json:
{status: 'load', message:'load'}
so everything is working as you described in controller. If you want to return user object, then change it to something like:
render json: { status: 'load', message:'load', user: 'user' }, status: :ok
it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object
– adarsh
Nov 14 '18 at 9:35
i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296141%2fhow-can-i-show-user-object-data-by-passing-user-authentication-token-in-rails-5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are passing 'user'
as a string to your json. So it will just send that string.
You could try doing something like:
render json: {status: 'load', message:'load', user: user.to_json},status: :ok
Or even:
render json: user
That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.
edit
OK, from your comment I think you probably need something along the lines of:
def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end
users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help
– adarsh
Nov 14 '18 at 12:20
add a comment |
You are passing 'user'
as a string to your json. So it will just send that string.
You could try doing something like:
render json: {status: 'load', message:'load', user: user.to_json},status: :ok
Or even:
render json: user
That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.
edit
OK, from your comment I think you probably need something along the lines of:
def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end
users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help
– adarsh
Nov 14 '18 at 12:20
add a comment |
You are passing 'user'
as a string to your json. So it will just send that string.
You could try doing something like:
render json: {status: 'load', message:'load', user: user.to_json},status: :ok
Or even:
render json: user
That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.
edit
OK, from your comment I think you probably need something along the lines of:
def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end
You are passing 'user'
as a string to your json. So it will just send that string.
You could try doing something like:
render json: {status: 'load', message:'load', user: user.to_json},status: :ok
Or even:
render json: user
That will get rid of the status and message json attributes, but you could argue the status should be shown by the HTTP response code and you shouldn't need the message if you are passing the user as the response.
edit
OK, from your comment I think you probably need something along the lines of:
def index
if request.headers["authentication_token"].present?
user = User.find_by(authentication_token: request.headers["authentication_token"])
render json: user
else
render json: { .... whatever you want to render if not authorised }
end
end
edited Nov 14 '18 at 14:48
answered Nov 14 '18 at 10:07
A_rayBA_rayB
863
863
users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help
– adarsh
Nov 14 '18 at 12:20
add a comment |
users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help
– adarsh
Nov 14 '18 at 12:20
users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help
– adarsh
Nov 14 '18 at 12:20
users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users i am getting close but still i dnt get ans pls need help
– adarsh
Nov 14 '18 at 12:20
add a comment |
In successful case, you respond with the following json:
{status: 'load', message:'load'}
so everything is working as you described in controller. If you want to return user object, then change it to something like:
render json: { status: 'load', message:'load', user: 'user' }, status: :ok
it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object
– adarsh
Nov 14 '18 at 9:35
i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:22
add a comment |
In successful case, you respond with the following json:
{status: 'load', message:'load'}
so everything is working as you described in controller. If you want to return user object, then change it to something like:
render json: { status: 'load', message:'load', user: 'user' }, status: :ok
it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object
– adarsh
Nov 14 '18 at 9:35
i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:22
add a comment |
In successful case, you respond with the following json:
{status: 'load', message:'load'}
so everything is working as you described in controller. If you want to return user object, then change it to something like:
render json: { status: 'load', message:'load', user: 'user' }, status: :ok
In successful case, you respond with the following json:
{status: 'load', message:'load'}
so everything is working as you described in controller. If you want to return user object, then change it to something like:
render json: { status: 'load', message:'load', user: 'user' }, status: :ok
answered Nov 14 '18 at 9:15
sharipov_rusharipov_ru
490310
490310
it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object
– adarsh
Nov 14 '18 at 9:35
i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:22
add a comment |
it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object
– adarsh
Nov 14 '18 at 9:35
i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:22
it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object
– adarsh
Nov 14 '18 at 9:35
it show in postman like this = { "status": "load", "message": "load", "user": "user" } it not show user object
– adarsh
Nov 14 '18 at 9:35
i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:22
i am getting close but still i dnt get ans pls need help // users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296141%2fhow-can-i-show-user-object-data-by-passing-user-authentication-token-in-rails-5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
i am getting close but still i dnt get ans pls need help == users = request.headers["authentication_token"].present? User.find_by(authentication_token: request.headers["authentication_token"]) render json: users
– adarsh
Nov 14 '18 at 12:24