Question: fields_for equivalent in android for rails api for nested attributes

Question

fields_for equivalent in android for rails api for nested attributes

Answers 0
Added at 2016-12-31 20:12
Tags
Question

There are two models:

User, Address. relationship: has_one

class User < ApplicationRecord
    has_one :address
    accepts_nested_attributes_for :address
end

class Address < ApplicationRecord
  belongs_to :user
end

This is how users_controller has the strong params:

def user_params
    params.require(:user).permit(:name, :mobile_number, :address_attributes => [:city, :pincode] )
  end

and the create method

def create
  @user = User.new(user_params)
  if (@user.save)
    respond_to do |format|
    format.json { head :ok }
  end
  end

The thing is I'm not using fields_for in the rails view. I'm not using rails_view at all, these params are not coming from rails view but from android, in this form,

{
        "name": "PP",
        "mobile_number": "9999999899",
        "address": {
            "city": "Ludhiana",
            "pincode": 123456
        }
    }

If I see the logs, this is how it receives the params:

Parameters: {"name"=>"PP", "mobile_number"=>"9999999899", "address"=>{"city"=>"Ludhiana", "pincode"=>123456}, "user"=>{"name"=>"PP", "mobile_number"=>"9999999899"}}

If you see the user params is not created along with nested address params.

What should be the json format for nested_attributes for controller to be able to read?

Answers
Source Show
◀ Wstecz