0

I followed the settings described in active_model_serializers guide to couple a Rails 5 API with Ember 3 app. In Rails ShopLanguagesController, I modified shop_language_params method as described in the above guide:

private

  def shop_language_params
    #params.require(:shop_language).permit(:shop_id, :language_id, :modified_by)
    ActiveModelSerializers::Deserialization.jsonapi_parse!(params, only: [:shop_id, :language_id, :modified_by] )
  end

Here is what I get when I post the following data from Ember app (seen in Rails log console):

Started POST "/shops/613/languages" for 127.0.0.1 at 2018-04-09 16:53:05 +0200
Processing by ShopLanguagesController#create as JSONAPI
  Parameters: {"data"=>{"attributes"=>{"modified_by"=>"Z28SCAMB"}, "relationships"=>{"shop"=>{"data"=>{"type"=>"shops", "id"=>"613"}}, "language"=>{"data"=>{"type"=>"languages", "id"=>"374"}}}, "type"=>"shop-languages"}, "shop_id"=>"613"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."username" = $1 LIMIT $2  [["username", "Z28SCAMB"], ["LIMIT", 1]]
  Shop Load (0.4ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = $1 LIMIT $2  [["id", 613], ["LIMIT", 1]]
++++ params: {:modified_by=>"Z28SCAMB"}

The posted data seems to be correct. How to extract the needed parameters (for example, language_id) from this JSON ?

belgoros
  • 3,590
  • 7
  • 38
  • 76

2 Answers2

0

Parsing JSON in Rails is quite straightforward:

parsed_json = ActiveSupport::JSON.decode(your_json_string)

Let's suppose, the object you want to associate the shortUrl with is a Site object, which has two attributes - short_url and long_url. Than, to get the shortUrl and associate it with the appropriate Site object, you can do something like:

parsed_json["results"].each do |longUrl, convertedUrl|
  site = Site.find_by_long_url(longUrl)
  site.short_url = convertedUrl["shortUrl"]
  site.save
end
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • Thank you, Mauricio for the response. What confuses me the most is the lack of examples and documentation on the Rails side. I believed that active_model_serializers gem will be smart enough to do all this hard parsing. If not it has not so much utility in a controller. – belgoros Apr 09 '18 at 19:45
0

The solution that worked for me was to modify the private shop_language_params method in ShopLanguagesController as follows:

def shop_language_params
    ActiveModelSerializers::Deserialization.jsonapi_parse!(params, only: [:shop, :language, :modified_by] )
end

As you see, I permit not foreign keys values for shop and language (shop_id and language_id) but the objects themselves: :shop and :language. Now you have shop_id and language_id values available in params hash as usually.

And, of course, you should call shop_language_params everywhere in the controller where you need do pass the shop_language params. Example where I find a language in the same controller:

private 

def find_language
    @language = Language.find_by!(id: shop_language_params[:language_id])
end
belgoros
  • 3,590
  • 7
  • 38
  • 76