-1

I Can't save model.

Company Model : Document Model = 1:N

Use a JSON, I wonder cyaan get params of Documents. But I don't get it. Does it understand where is bad? Please let me know.

Post Data

{
  "name": "name",
  "address": "asd",
  "phone": "0394564",
  "fax": "168465",
  "mail": "mailtest",
  "documents":[{
    "name": "name",
    "address": "asd",
    "phone": "58465",
    "fax": "543543"
},{
    "name": "test",
    "address": "asd",
    "phone": "5452452",
    "fax": "24534535"
}]
}

Code [Model]company.rb

class Company < ActiveRecord::Base 
  has_many :documents
  accepts_nested_attributes_for :documents
end

[Model]document.rb

class Document < ActiveRecord::Base
  belongs_to :company
end

[Controller]company_controller.rb

def create
  @company = Company.new(company_params) 

  if @company.save 
    render json: @company, status: :created
  else
  render json: @company.errors, status: :unprocessable_entity
end

end

def company_params 
  params.require(:company).permit(:name, :address, :phone, :fax, :mail, documents_attributes: [:name])
end

LOG example

Started POST "/api/v1/companies/" for 192.168.1.19 at 2014-06-04 21:32:46 +0900
Processing by Api::V1::CompaniesController#create as JSON
  Parameters: {"name"=>"name", "address"=>"asd", "phone"=>"12345", "fax"=>"12345", "mail"=>"mailtest", "documents"=>[{"name"=>"name", "address"=>"asd", "phone"=>"12345", "fax"=>"12345"}, {"name"=>"name", "address"=>"asd", "phone"=>"12345", "fax"=>"12345"}], "company"=>{"name"=>"name", "address"=>"asd", "phone"=>"12345", "fax"=>"12345", "mail"=>"mailtest"}}
   (0.1ms)  begin transaction
  SQL (1.8ms)  INSERT INTO "companies" ("address", "created_at", "fax", "mail", "name", "phone", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["address", "asd"], ["created_at", "2014-06-04 12:32:46.583770"], ["fax", "12345"], ["mail", "mailtest"], ["name", "name"], ["phone", "12345"], ["updated_at", "2014-06-04 12:32:46.583770"]]
   (3.8ms)  commit transaction
12
  • 1
    Do you get eny errors in the response?
    – BroiSatse
    Jun 5, 2014 at 11:16
  • Do your params come in the form { "company": { ... } } Jun 5, 2014 at 11:19
  • Get no Error , add LOG example.
    – Shinya
    Jun 5, 2014 at 11:22
  • 3
    In your POST Data section, the nested attributes appear to be under the petshops field. In your strong parameters declaration, they are called documents_attributes, and in your log, they appear to be just documents. This is probably (the beginning of) your problem. These should all be documents_attributes.
    – amnn
    Jun 5, 2014 at 11:22
  • post add "company:" ParamsParser::ParseError
    – Shinya
    Jun 5, 2014 at 11:23

1 Answer 1

1

You have to declare object as long as you are requiring it in the strong params.

{"company"=>{"name"=>...}}

Rails will catch the "company" value and permit them.

Same problem here:

Why isn't my Rails API saving anything to the database?

Edit:

In your params:

..
"documents":[{
..

In your permits:

   documents_attributes:

Maybe this is the reason.

Related info:

http://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through

Rails 4 nested attributes multiple records when updating

3
  • 1
    thx! I try this format. It was not able to do too. bud I got a param parmit error. I think that it leads to solution.
    – Shinya
    Jun 5, 2014 at 15:11
  • I'm gonna add more info to this answer. Jun 5, 2014 at 15:55
  • It has solved. Thanks Developers & Scholars. The JSON Format of the cause was bad.
    – Shinya
    Jun 6, 2014 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.