Rails 5 Api create new object from json with nested resource











up vote
0
down vote

favorite












this is the json received as parameters from external angular webapp:



{
"provincia": {
"id": 1,
"name": "Province"
},
"username": "tester",
"direccion": "new avenue 100",
"email": "nomail@mail.com"
}


this is my controller



 def create
@seller = Seller.new(seller_params)

if @seller.save
render json: @seller, status: :created, location: @seller
else
puts @seller.errors.full_messages
render json: @seller.errors, status: :unprocessable_entity
end
end


this is seller_params



def seller_params
params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
end


models: Seller belongs_to Provincia



server console output error full message



Provincia must exist


Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.










share|improve this question




























    up vote
    0
    down vote

    favorite












    this is the json received as parameters from external angular webapp:



    {
    "provincia": {
    "id": 1,
    "name": "Province"
    },
    "username": "tester",
    "direccion": "new avenue 100",
    "email": "nomail@mail.com"
    }


    this is my controller



     def create
    @seller = Seller.new(seller_params)

    if @seller.save
    render json: @seller, status: :created, location: @seller
    else
    puts @seller.errors.full_messages
    render json: @seller.errors, status: :unprocessable_entity
    end
    end


    this is seller_params



    def seller_params
    params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
    end


    models: Seller belongs_to Provincia



    server console output error full message



    Provincia must exist


    Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      this is the json received as parameters from external angular webapp:



      {
      "provincia": {
      "id": 1,
      "name": "Province"
      },
      "username": "tester",
      "direccion": "new avenue 100",
      "email": "nomail@mail.com"
      }


      this is my controller



       def create
      @seller = Seller.new(seller_params)

      if @seller.save
      render json: @seller, status: :created, location: @seller
      else
      puts @seller.errors.full_messages
      render json: @seller.errors, status: :unprocessable_entity
      end
      end


      this is seller_params



      def seller_params
      params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
      end


      models: Seller belongs_to Provincia



      server console output error full message



      Provincia must exist


      Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.










      share|improve this question















      this is the json received as parameters from external angular webapp:



      {
      "provincia": {
      "id": 1,
      "name": "Province"
      },
      "username": "tester",
      "direccion": "new avenue 100",
      "email": "nomail@mail.com"
      }


      this is my controller



       def create
      @seller = Seller.new(seller_params)

      if @seller.save
      render json: @seller, status: :created, location: @seller
      else
      puts @seller.errors.full_messages
      render json: @seller.errors, status: :unprocessable_entity
      end
      end


      this is seller_params



      def seller_params
      params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
      end


      models: Seller belongs_to Provincia



      server console output error full message



      Provincia must exist


      Which modification in the Rails API should I do to make it work, and save the new seller? thanks in advance.







      ruby-on-rails ruby angular api ruby-on-rails-5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago

























      asked 1 hour ago









      matQ

      1149




      1149
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          The way that you are permiting your params in the controller are not correct:



          Rails Docs



          You need to pass your provincia_id in your attributes or permit that params that are you passing by



          Way 1:



          {
          "provincia_attributes": {
          "id": 1,
          "name": "Province"
          },
          "username": "tester",
          "direccion": "new avenue 100",
          "email": "nomail@mail.com"
          }


          SellersController.rb



          def seller_params
          params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
          end


          Way 2



          {
          "provincia_id": "1"
          "username": "tester",
          "direccion": "new avenue 100",
          "email": "nomail@mail.com"
          }


          SellersController.rb



          def seller_params
          params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
          end





          share|improve this answer








          New contributor




          Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

























            up vote
            0
            down vote













            Try changing the params like this:



            def seller_params
            p = params.require(:seller).premit(:username, :direccion, :email).to_h
            p[:seller][:provincia_id] = params[:seller][:provincia][:id]
            p
            end


            That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343652%2frails-5-api-create-new-object-from-json-with-nested-resource%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              The way that you are permiting your params in the controller are not correct:



              Rails Docs



              You need to pass your provincia_id in your attributes or permit that params that are you passing by



              Way 1:



              {
              "provincia_attributes": {
              "id": 1,
              "name": "Province"
              },
              "username": "tester",
              "direccion": "new avenue 100",
              "email": "nomail@mail.com"
              }


              SellersController.rb



              def seller_params
              params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
              end


              Way 2



              {
              "provincia_id": "1"
              "username": "tester",
              "direccion": "new avenue 100",
              "email": "nomail@mail.com"
              }


              SellersController.rb



              def seller_params
              params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
              end





              share|improve this answer








              New contributor




              Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                1
                down vote













                The way that you are permiting your params in the controller are not correct:



                Rails Docs



                You need to pass your provincia_id in your attributes or permit that params that are you passing by



                Way 1:



                {
                "provincia_attributes": {
                "id": 1,
                "name": "Province"
                },
                "username": "tester",
                "direccion": "new avenue 100",
                "email": "nomail@mail.com"
                }


                SellersController.rb



                def seller_params
                params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
                end


                Way 2



                {
                "provincia_id": "1"
                "username": "tester",
                "direccion": "new avenue 100",
                "email": "nomail@mail.com"
                }


                SellersController.rb



                def seller_params
                params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
                end





                share|improve this answer








                New contributor




                Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The way that you are permiting your params in the controller are not correct:



                  Rails Docs



                  You need to pass your provincia_id in your attributes or permit that params that are you passing by



                  Way 1:



                  {
                  "provincia_attributes": {
                  "id": 1,
                  "name": "Province"
                  },
                  "username": "tester",
                  "direccion": "new avenue 100",
                  "email": "nomail@mail.com"
                  }


                  SellersController.rb



                  def seller_params
                  params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
                  end


                  Way 2



                  {
                  "provincia_id": "1"
                  "username": "tester",
                  "direccion": "new avenue 100",
                  "email": "nomail@mail.com"
                  }


                  SellersController.rb



                  def seller_params
                  params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
                  end





                  share|improve this answer








                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  The way that you are permiting your params in the controller are not correct:



                  Rails Docs



                  You need to pass your provincia_id in your attributes or permit that params that are you passing by



                  Way 1:



                  {
                  "provincia_attributes": {
                  "id": 1,
                  "name": "Province"
                  },
                  "username": "tester",
                  "direccion": "new avenue 100",
                  "email": "nomail@mail.com"
                  }


                  SellersController.rb



                  def seller_params
                  params.require(:seller).permit(:username, :direccion, :email, provincia_attributes: [:id, :name])
                  end


                  Way 2



                  {
                  "provincia_id": "1"
                  "username": "tester",
                  "direccion": "new avenue 100",
                  "email": "nomail@mail.com"
                  }


                  SellersController.rb



                  def seller_params
                  params.require(:seller).permit(:username, :direccion, :email, :provincia_id)
                  end






                  share|improve this answer








                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 39 mins ago









                  Leonardo da Rosa

                  112




                  112




                  New contributor




                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Leonardo da Rosa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.
























                      up vote
                      0
                      down vote













                      Try changing the params like this:



                      def seller_params
                      p = params.require(:seller).premit(:username, :direccion, :email).to_h
                      p[:seller][:provincia_id] = params[:seller][:provincia][:id]
                      p
                      end


                      That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        Try changing the params like this:



                        def seller_params
                        p = params.require(:seller).premit(:username, :direccion, :email).to_h
                        p[:seller][:provincia_id] = params[:seller][:provincia][:id]
                        p
                        end


                        That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Try changing the params like this:



                          def seller_params
                          p = params.require(:seller).premit(:username, :direccion, :email).to_h
                          p[:seller][:provincia_id] = params[:seller][:provincia][:id]
                          p
                          end


                          That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.






                          share|improve this answer












                          Try changing the params like this:



                          def seller_params
                          p = params.require(:seller).premit(:username, :direccion, :email).to_h
                          p[:seller][:provincia_id] = params[:seller][:provincia][:id]
                          p
                          end


                          That will add the "provincia_id" key you are missing for the association. I call to_h to get a new hash because I don't like to mutate the original params and you have already permited the values you want so that Hash is safe to use.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 40 mins ago









                          arieljuod

                          5,63911121




                          5,63911121






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343652%2frails-5-api-create-new-object-from-json-with-nested-resource%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                              Alcedinidae

                              RAC Tourist Trophy