active-model-serializers

Ember: error.messages does not show server errors on save

假装没事ソ 提交于 2020-01-02 07:27:28
问题 When trying to create a new record, the errors.messages do not render as described in the docs. That said, the console does render the error Error: The backend rejected the commit because it was invalid: {email: has already been taken} . I have the following in my ember-cli app: Router Router.map -> this.route 'users/new', path: '/signup' Route UsersNewRoute = Ember.Route.extend( model: -> return @store.createRecord('user') ) Controller UsersNewController = Ember.ObjectController.extend(

Active Model Serializers belongs_to

孤人 提交于 2019-12-31 09:11:50
问题 This question pertains to AMS 0.8 I've got two models: class Subject < ActiveRecord::Base has_many :user_combinations has_ancestry end class UserCombination < ActiveRecord::Base belongs_to :stage belongs_to :subject belongs_to :user end And two serializers: class UserCombinationSerializer < ActiveModel::Serializer attributes :id belongs_to :stage belongs_to :subject end class SubjectSerializer < ActiveModel::Serializer attributes :id, :name, :description, :subjects def include_subjects?

JSON rendering with activemodel serializer in Rails

给你一囗甜甜゛ 提交于 2019-12-29 07:48:07
问题 I just wonder how the rails render the model objects with activemodel-serializer in JSON. I installed activemodel-serializer but the output is something different. The ideal is something like: "products": [ { "id": 1, "title": "Digital Portable System", }, { "id": 2, "title": "Plasma TV", } ] However what I got so far is; My code is simple; class Api::V1::ProductsController < ApplicationController before_action :authenticate_with_token!, only: [:create, :update, :destroy] respond_to :json def

Active Model Serializers data attributes from associations are not loading

不想你离开。 提交于 2019-12-25 06:57:59
问题 I am trying to load the association as follows: This is my controller which is trying to render the json. #app/controllers/store/products_controller.rb def customize @option_types = OptionType.all respond_to do |format| format.json { render :json => @option_types} end end This is the serializer for above object #app/serializers/option_type_serializer.rb class OptionTypeSerializer < ActiveModel::Serializer attributes :id, :key, :customizable, :name has_many :option_values end option_value

Ember 2.7, Rails 5, JSONAPI, Active Model Serializers - counting the number of records in a hasMany relationship

感情迁移 提交于 2019-12-25 04:48:12
问题 This idea started with this question. 2 models: class Trail < ApplicationRecord has_many :notes, dependent: :destroy end class Note < ApplicationRecord belongs_to :trail end 2 serializers: class NotesSerializer < ActiveModel::Serializer attributes :id, :note_body belongs_to :trail end class TrailSerializer < ActiveModel::Serializer attributes :id, :name, :notes_count has_many :notes def notes_count object.notes.size end end When calling the route trails/index, this will fire for every Trail,

Get kaminari pagination links in the JSON generated by the active model serializer

泪湿孤枕 提交于 2019-12-24 16:33:54
问题 I am trying to convert @admins to JSON using the AdminSerializer #app/serializers/admin_serializer.rb class AdminSerializer < ActiveModel::Serializer attributes :id, :email, :access_locked? end where Admins is>> @admins = @search.result(:distinct => true).page(params[:page][:number]).per(10) and @search = Admin.search(params[:q]) When I execute this command>> ActiveModel::SerializableResource.new(@admins.to_a).as_json I do get the desired JSON, but the pagination links are missing from the

Ember Data belongsTo Association (JSON format?)

这一生的挚爱 提交于 2019-12-24 13:08:04
问题 I have two models 'Author' and 'Publisher' (Rails), with a publisher hasOne author / author belongsTo publisher relationship. I have the Ember models setup correctly -- JS Fiddle -- and the associations working when I manually push into the store. But only the publisher records are created when requesting /publishers index. I've tried several types of JSON responses: Publishers with author { "publishers": [ { "id": 1, "name": "Test P 1", "author": 1 } ], "author": { "id": 1, "name": "Test A 1

Post to Rails from Emberjs using JSONAPI adapter, Rails not seeing the params

断了今生、忘了曾经 提交于 2019-12-23 12:21:07
问题 I am trying to save a message model in Emberjs. I am using the JSONAPIAdapter and JSONAPISerilzier. My post to rails hits the right controller and action as a post, but if I look inside with Pry, the data attributes are not there. My payload: {"data":{"attributes":{"body":"Why","user_id":"17"},"relationships":{"user":{"data":null},"conversation":{"data":null}},"type":"messages"}} Content-Type:application/vnd.api+json Rails params in the Rails console: {"format"=>"json", "controller"=>"api/v1

Rails 3.2, saving serialized hash will not save number_with_delimiter()

旧巷老猫 提交于 2019-12-23 02:28:50
问题 It appears that in Rails 3.2.21, saving a serialized hash fails to save a value that comes from one specifc NumberHelper, helper.number_with_delimiter In a Rails 3.2 app, in model Foo I have: serialize :stuff, Hash In the console: f = Foo.create f.stuff = { a: "aaaa", b: 1111, c: helper.number_with_delimiter(123456) } => {:a=>"aaaa", :b=>1111, :c=>"123,456"} # so far so good f.save! f.stuff => {:a=>"aaaa", :b=>1111, :c=>123456} # c should be a STRING It DOES work correctly with helper.number

Serializing the errors hash in ActiveModel::Serializer

独自空忆成欢 提交于 2019-12-20 19:46:23
问题 I'm using ActiveModel::Serializer to customize the JSON responses for my API. This works fine in most cases, except when it fails to save a model successfully. For example, def create def create book = Book.new(book_params) book.save respond_with book, location: nil end end As I understand it, the respond_with action will basically execute code that looks something like this (in order to generate the response). if resource.errors.any? render json: {:status => 'failed', :errors => resource