<FromGitter>
<Sjoerrdd> and why can't i send validation errors as a json response?
feepbot has quit [Ping timeout: 252 seconds]
feepbot has joined #amber
<FromGitter>
<marco-fp> can you be more specific? Why can't you?
<FromGitter>
<marco-fp> would be awesome to be able to disable the npm build and webpack stuff from the new apps, btw.
<FromGitter>
<Sjoerrdd> Nevermind, fixed it
<FromGitter>
<Sjoerrdd> I'm creating a SPA, so I linked all routes to `HomeController, :index`
<FromGitter>
<Sjoerrdd> But it's not possible to get my static files \o/
<FromGitter>
<Sjoerrdd> because * goes to the controller, and * goes to the public path
<FromGitter>
<Sjoerrdd> because (STAR) goes to the controller, and (STAR) goes to the public path*
<FromGitter>
<marco-fp> I'd advice you to not use amber to serve the SPA
<FromGitter>
<marco-fp> use nginx for that :)
<FromGitter>
<marco-fp> and Amber for the api endpoints
<FromGitter>
<marco-fp> it should be fast enough to cover you for low loads, but ideally you should use a dedicated server engine for that and not a single process/thread as Amber
<FromGitter>
<drujensen> @Sjoerrdd The public path uses the static pipeline and is already defined so you don’t need to create your own. The recommended way to setup an API would be to use the following routes: ⏎ ⏎ ```code paste, see link``` [https://gitter.im/amberframework/amber?at=5bbf60db3844923661202663]
<FromGitter>
<drujensen> @marco-fp is correct that using nginx is best for delivery of assets. I usually use docker compose and setup an nginx that maps a volume to the static assets.
<FromGitter>
<drujensen> or you can push them to the edges of a CDN(content delivery network) for even better performance
<FromGitter>
<johansmitsnl> thats because the result is empty (not in the db yet)
<FromGitter>
<Blacksmoke16> make it `.first?` should return nil instead of raising
<FromGitter>
<johansmitsnl> That did it, thnx 👍
<FromGitter>
<Blacksmoke16> np
<FromGitter>
<johansmitsnl> How would I override a setter for `Granite::Base` ⏎ ⏎ I want to downcase the email field in the model, I made this function: ⏎ ⏎ ```def email=(input) ⏎ @email = input.downcase ⏎ end``` ... [https://gitter.im/amberframework/amber?at=5bbf90e5271506518df5bddb]
<FromGitter>
<Blacksmoke16> how are you calling it?
<FromGitter>
<johansmitsnl> from the controller action (that was scaffold)
<FromGitter>
<Thellior> You see that we have @{{name.id}}
<FromGitter>
<Thellior> So in the email example
<FromGitter>
<Thellior> It will be @email = value.to_s
<FromGitter>
<Thellior> So it’s not calling the method directly but assign the value directly
<FromGitter>
<Blacksmoke16> `account.set_attributes account_params.validate!` after this line you could prob do your `account.email = "abc@gmail.com"` which *should* call your setter
<FromGitter>
<Thellior> That also works
<FromGitter>
<Thellior> We could also change it to use the setter instead
<FromGitter>
<Thellior> We are creating the methods anyways
<FromGitter>
<Blacksmoke16> ye, they should already be made by then
<FromGitter>
<Thellior> And if people override them they get expected behaviour
<FromGitter>
<Blacksmoke16> that whole cast_to_field needs redone anyway is a mess
<FromGitter>
<Thellior> I will make a pull request
<FromGitter>
<johansmitsnl> Understanding it now ⏎ I know it not Rails (thats why i'm learning it) but it looks a lot like it so there are some expectations that are different. And thats fine I'm just discovering them 😄
<FromGitter>
<Thellior> put .json to the end of the url if im correct
<FromGitter>
<Thellior> Or set the content-type request header if im correct out of my head
<FromGitter>
<Thellior> to application/json
<FromGitter>
<robacarp> yeah, how are you calling that url? just plain in the browser won't work because it doesn't have the accepts header set to json
<FromGitter>
<johansmitsnl> I did it in the browser yes
<FromGitter>
<johansmitsnl> It does not look to the extension that is requested then?
<FromGitter>
<robacarp> 1 sec, let me look this up
<FromGitter>
<Thellior> @Blacksmoke16 Removing the @ will raise a lot of errors so i keep it like this for now
<FromGitter>
<Blacksmoke16> 👍
<FromGitter>
<johansmitsnl> When I do the request with the json accept header it returns the json indeed
<FromGitter>
<johansmitsnl> What is the way to select only specific fields that you want to json? Now I have this: `nodes.to_json` but I want to have only `id, hostname` in the result.
<FromGitter>
<Blacksmoke16> using ignore: true could be an option, just know that it would be ignored both ways
<FromGitter>
<johansmitsnl> Defining it in ignore will work for now, but it will be later on an issue, because depending on the current user I want to expose more fields
<FromGitter>
<Blacksmoke16> i.e. if you have a password field and do like `Model.from_json(%({"id":1, "password": "foo"}))` it wouldn't parse the `password` field into your object
<FromGitter>
<Blacksmoke16> overriding the `.to_json` method and having logic in there would be best bet imo
<FromGitter>
<Blacksmoke16> at least then you would retain the being able to get the fields from the json into your object, and manually do that to have better control over it
<FromGitter>
<Blacksmoke16> i wish there was a better way to do stuff like this. is a major pita to do manually, esp with a lot of fields :/
<FromGitter>
<paulcsmith> But I wonder if you've tried something you like that works better
<FromGitter>
<robacarp> my personal philosophy is that customized serializers should be avoided -- whatever a user object is, should always be a user object
<FromGitter>
<robacarp> that way client devs dont have to have a user_from_show_deserializer and a user_from_index_deserializer
<FromGitter>
<robacarp> so I'd just serialize the object into the array for #index
<FromGitter>
<paulcsmith> That's basically what is done in most IndexSerializers, but I often need to add extra data, like the total count, what page you are on, total pages, etc. for the index
<FromGitter>
<robacarp> oh, for sure, metadata
<FromGitter>
<paulcsmith> How do you handle that?
<FromGitter>
<robacarp> In Amber? unfortunately not well :<
<FromGitter>
<robacarp> Granite doesn't really populate that stuff in a way that would be convienient to publish
<FromGitter>
<paulcsmith> Oh I see. So for now are you doing something like this in index actions? `json users.map({ |user| UserSerializer.serialize(user) })` and leaving out metadata?
<FromGitter>
<robacarp> essentially, yes
<FromGitter>
<robacarp> well, you can't have an array at the top level in json right?