ruby on rails - Json file output is [null, null, null, null] -
i'm trying save , output data website when check movies.json file shows,
[null,null,null,null,null,null,null,null]
that's null every id in database.
this log rails server,
started "/movies.json" 127.0.0.1 @ 2015-08-25 07:52:48 +0200 activerecord::schemamigration load (0.1ms) select "schema_migrations".* "schema_migrations" processing moviescontroller#index json user load (0.4ms) select "users".* "users" "users"."id" = ? order "users"."id" asc limit 1 [["id", 1]] movie load (0.1ms) select "movies".* "movies" completed 200 ok in 116ms (views: 10.1ms | activerecord: 4.5ms)
in application_controller.rb have
respond_to :html, :json
my movies_controller.rb
class moviescontroller < applicationcontroller def index respond_with movie.all end def create respond_with movie.create(movie_params) end private def movie_params params.require(:movie).permit(:title) end end
my movie.rb model,
class movie < activerecord::base def as_json(options = {}) end end
if check movie.all
in rails console see correct values, it's not outputting right in json output.
you overwriting as_json
empty implementation returns nothing.
if need overwrite default serializer, have specify fields want serialized
def as_json(options = {}) { :username => username, :name => first_name + " " + last_name } end
if default implementation enough you, remove
def as_json(options = {}) end
Comments
Post a Comment