I'm trying to render some json in my rails app using jbuilder, but the output is showing up as:
{"status":"500","error":"Internal Server Error"}
Here's the url:
http://localhost:3000/api/v1/appusers/10
Here's the controller:
module Api
module V1
class AppusersController < ApplicationController
respond_to :json
skip_before_action :verify_authenticity_token
def show
#appuser = Appuser.find(params[:id])
end
And my show.json.jbuilder file:
json.extract! #appuser, :user_auth_token, :id
end
I've never encountered this before with jbuilder and all of my other jbuilder files work just fine. Any idea what I'm missing?
According to documentation, you don't need the end in the jbuilder template, just:
json.extract! #appuser, :user_auth_token, :id
Alternatively, if you are using a Ruby version major than 1.9, you can use this syntax:
json.(#appuser, :user_auth_token, :id)
Related
I'm building a simple rails application where users can upload some images. Now I need a simple controller that returns the image's URLs of related record which have the same id_attivita. To do so I create a function in the controller and enable it in the routes.rb file.
My question is about how to respond to the http request with the attribute value of image.url provided from paperclip?
def getattached
#photo_attivitum = PhotoAttivitum.where(id_attivita: params[:id_attivita])
respond_to do |format|
#photo_attivitum.each do |p|
format.html { render :json => p.image.url}
end
end
end
it works but it returns only the URLs of the first record not the other four record's URLs...
How can I do this?
Add the following gem to your Gemfile
gem 'active_model_serializers'
Then install it using bundle
bundle install
You can generate a serializer as follows
rails g serializer photo_attivitum
it will create Serializer class file in
# app/serializers/photo_attivitum_serializer.rb
class PhotoAttivitumSerializer < ActiveModel::Serializer
attributes :id, :image_url
def image_url
object.image.url
end
end
And in controller
def getattached
#photo_attivitum = PhotoAttivitum.where(id_attivita:
params[:id_attivita])
render json: #photo_attivitum
end
Not sure what u want to do? Show all image urls in json?
urls = #photo_attivitum.pluck('image_url')
format.html { render :json => urls}
I am upgrading my Rails app from 4.1.1 to 5.1.4.
I am using roar-rails gem to parsing and rendering REST documents. I am facing some issues as responders gem has been extracted to separate gem.
respond_with has been moved to 'responders' gem.
My rails 4 code lookgs like this:
PostsController:
class PostsController < ApplicationController
respond_to :json
def index
posts = current_user.posts
respond_with posts, :represent_with => PostsRepresenter, current_user: current_user
end
end
My representers for Post
module PostsRepresenter
# Rails 4 code
# include Roar::Representer::JSON::HAL
# Rails 5 code (after adding responders) --------
include Roar::JSON
include Roar::Hypermedia
# Rails 5 code --------
collection(
:post,
class: Post,
extend: PostRepresenter,
embedded: true)
link :make do |args|
p "............. #{args.inspect}"
# In rails 4, args are coming correctly what is passing from Controller
# But in rails 5, args is coming `nil`
posts_path if args[:current_user].can_create?(Post)
end
end
Post representer
module PostRepresenter
# Rails 4 code
# include Roar::Representer::JSON::HAL
# Rails 5 code (after adding responders) --------
include Roar::JSON
include Roar::Hypermedia
# Rails 5 code --------
property :title
property :description
property :author
link :self do |args|
post_path(id) if args[:current_user].can_read?(self)
end
link :remove do |args|
post_path(id) if args[:current_user].can_delete?(self)
end
link :edit do |args|
post_path(id) if args[:current_user].can_update?(self)
end
end
I am facing issue with args which are passing through Controller,
after rails 5, its coming nil.
I have debug the issue and found that in responders gem, options are coming in respond_with method, but I think it could not send it to roar-rails.
/path-to-gem/vendor/responders-master/lib/action_controller/respond_with.rb
Here is snippet:
def respond_with(*resources, &block)
if self.class.mimes_for_respond_to.empty?
raise "In order to use respond_with, first you need to declare the " \
"formats your controller responds to in the class level."
end
mimes = collect_mimes_from_class_level
collector = ActionController::MimeResponds::Collector.new(mimes, request.variant)
block.call(collector) if block_given?
if format = collector.negotiate_format(request)
_process_format(format)
options = resources.size == 1 ? {} : resources.extract_options!
options = options.clone
options[:default_response] = collector.response
p "====================== options :: #{options.inspect}"
# Options are correct till here but coming `nil` on representers
(options.delete(:responder) || self.class.responder).call(self, resources, options)
else
raise ActionController::UnknownFormat
end
end
Please let me know what needs to be done here that make args
available in representers
respond_with was deprecated from rails 4.2.1.
https://apidock.com/rails/ActionController/MimeResponds/respond_with
I have a simple model:
class Receipt
include ActiveModel::Serialization
attr_accessor :products
end
and my controller is doing:
def create
respond_with receipt, :serializer => ReceiptSerializer
end
and the serializer:
class ReceiptSerializer < ActiveModel::Serializer
attributes :products
end
and I get:
NoMethodError:
undefined method `to_model' for #<Receipt:0x007f99bcb3b6d8>
Yet if I change my controller to:
def create
json = ReceiptSerializer.new(receipt)
render :json => json
end
Then everything works fine... what is happening???
I was using active_model_serializers 0.9.3, but just tried 0.10.2, and the results are the same.
In all the documentation I've read and personal implementation I use render json: instead of respond_with.
render json: receipt, serializer: ReceiptSerializer
I believe that respond_with has been removed from rails and isn't considered a best practice anymore but I can't find a link to validate that claim.
I'm not totally sure, but it seems in your Receipt PORO, you should rather include: ActiveModel::SerializerSupport.
I can't confirm if that works for active_model_serializers 0.10.2 though
I'd like a Rails controller (all of them, actually, it's an API) to render JSON always always.
I don't want Rails to return "route not found", or try and fail to find an HTML template, or return 406. I just want it to automatically and always render JSON, e.g. from a RABL or JBuilder view.
Is this possible? Related questions seem to have answers that have the aforementioned downsides.
You can add a before_filter in your controller to set the request format to json:
# app/controllers/foos_controller.rb
before_action :set_default_response_format
protected
def set_default_response_format
request.format = :json
end
This will set all response format to json. If you want to allow other formats, you could check for the presence of format parameter when setting request.format, for e.g:
def set_default_response_format
request.format = :json unless params[:format]
end
You can use format.any:
def action
respond_to do |format|
format.any { render json: your_json, content_type: 'application/json' }
end
end
It's just:
render formats: :json
I had similar issue but with '.js' extension. To solve I did the following in the view:
<%= params.except!(:format) %>
<%= will_paginate #posts %>
I tried the above solutions and it didn't solve my use case.
In some of the controllers of my Rails 4.2 app, there was no explicit render called. For example, a service object was called and nothing was returned. Since they are json api controllers, rails was complaining with a missing template error. To resolve I added this to our base controller.
def render(*args)
options = args.first
options.present? ? super : super(json: {}, status: :ok)
end
It's a large app I'm converting to Rails 5, so this is just a safety measure as I removed the RocketPants gem that seemed to do this automatically.
As a note, my controllers inherit from ActionController::Base
Of course:
before_filter :always_json
protected
def always_json
params[:format] = "json"
end
You should probably put this in a root controller for your API.
I created a practice rails app where I have created a namespace and versioned like is demonstrated in this railscast. Everything is working fine and I can see the json output in the browser
Then I added the Rabl gem and was trying to render the rabl views but I get an empty JSON array rendered in the browser
Here is what I did systematically to get the versioning working
1) changed the routes file to look like this
App0828::Application.routes.draw do
namespace :api, defaults: { format: 'json'} do
namespace :v1 do
resources :vendors
end
end
#resources :vendors
#root to: 'vendors#index'
end
2) The created thise files
app/copntrollers/api/v1/vendors_controller.rb
Inside the vendors_controller.rb I added the following code
module Api
module V1
class VendorsController < ApplicationController
class Vendor < ::Vendor
#subclass vendor class so thatyou can extend its behaviour just for this version
#add any functions specific to this version here
end
respond_to :json
def index
respond_with Vendor.all
end
def show
respond_with Vendor.find(params[:id])
end
..........
3) Then I pointed my browser to this url "http://localhost:3000/api/v1/vendors"
And I can see the json output in my browser
4) Then I added the rabl gem
5) restarted the server
6) Changed the above file at app/copntrollers/api/v1/vendors_controller.rb
from the version above to the version below
module Api
module V1
class VendorsController < ApplicationController
class Vendor < ::Vendor
#subclass vendor class so thatyou can extend its behaviour just for this version
#add any functions specific to this version here
end
respond_to :json
def index
render 'api/v1/index.json.rabl'
end
def show
render 'api/v1/show.json.rabl'
end
.......
7) I created the following files with this code:
file: app/views/api/v1/show.json.rabl
code: object #vendor
attributes :id, :name
file: app/views/api/v1/index.json.rabl
code: object #vendors
attributes :id, :name
8) Routes file looks like this
api_v1_vendors GET /api/v1/vendors(.:format) api/v1/vendors#index {:format=>"json"}
POST /api/v1/vendors(.:format) api/v1/vendors#create {:format=>"json"}
new_api_v1_vendor GET /api/v1/vendors/new(.:format) api/v1/vendors#new {:format=>"json"}
edit_api_v1_vendor GET /api/v1/vendors/:id/edit(.:format) api/v1/vendors#edit {:format=>"json"}
api_v1_vendor GET /api/v1/vendors/:id(.:format) api/v1/vendors#show {:format=>"json"}
PUT /api/v1/vendors/:id(.:format) api/v1/vendors#update {:format=>"json"}
DELETE /api/v1/vendors/:id(.:format) api/v1/vendors#destroy {:format=>"json"}
9) Finally I went to url: "http://localhost:3000/api/v1/vendors.json"
And all I see in the browser is an empty JSON hash: "{}"
So clearly it cannot access the instance variables. Seems to be some issue with being out of scope. Im not sure how to proceed next. I couldn't find any examples online of versioning with rabl. Any suggestions? Id really appreciate it
Thanks
Where are you setting the instance variables?
First, you probably should have this line: respond_to :json in your Api::ApplicationController (or something like that, basically the controller from which every other controller in the Api module inherit).
You dont need these: render 'api/v1/index.json.rabl'.
Just set your instance variables:
def index
#vendors = Vendor.all
respond_with #vendors
end
def show
#vendor = Vendor.find(params[:id])
respond_with #vendor
end