0

I need to store the bellow parameter into a table:

[{"id":"1","name":"Cheese and red deans","amount":2,"price":"0.65"},{"id":"2","name":"Pork, cheese and red beans","amount":2,"price":"0.85"},{"id":"3","name":"Soda","amount":1,"price":"0.65"}]

The controller's code is this (I'm using ActiveController):

#POST /ordertemps
def create    
    @ordertemp = Ordertemp.new(ordertemp_params)
    if @ordertemp.save
        render json: @ordertemp
    else
        render error: { error: 'Unable to create an order'}, status: 400
    end
    
end

However, I'm getting an error in the next code:

private

def ordertemp_params
    params.require(:ordertemp).permit(:name, :amount, :price)
end

I found the method permit is just related to hashes, so apparently my data is not in that format, so I tried to use different functions such as: .each_with_index.to_a, to_h, Hash but nothing seems to work.

So my questions are:

  1. what function should I use to convert my data to a hash?
  2. Do I need to iterate my data in order to save each item into the table?

Thanks a lot

3
  • 1
    Is there an error? You should be able to work with ActionController::Parameters whatever is you're receiving, you can try adding :id to permit. If you need to process multiple params at once I think you'll have to adapt your controller code. Commented Jul 25, 2022 at 20:54
  • yes, apparently the method permit only works with hashes and I think my object is an array of arrays, on the other hand, you're right about the id field, I have to remove the itbecause at this stage it is not important for the backend Commented Jul 25, 2022 at 20:59
  • 1
    Although looks hacky, you could map the params and update the controller code; def create @ordertemp = ordertemp_params.map { |attrs| Ordertemp.new(attrs) } if @ordertemp.map(&:save).all?(true) render json: @ordertemp, status: :created, location: @ordertemp else render json: @ordertemp.map(&:errors), status: :unprocessable_entity end end; def ordertemp_params params.require(:ordertemp).map { |param| param.permit(:id, :name, :amount, :price) } end Commented Jul 25, 2022 at 21:16

1 Answer 1

1

You need to add more to your question. Go to your web console and copy the actual params being submitted. Also what exactly are your errors?

You can look at this: https://codersloth.medium.com/rails-creating-multiple-records-from-a-single-request-a45261085164 as it covers much of what you are talking about. I'm guessing here because of your lack of info in your question but this might work:

def create 
  begin  

    OrderTemp.transaction do
      @ordertemp = Ordertemp.create!(ordertemp_params)
    end

  rescue ActiveRecord::RecordInvalid => exception
    # omitting the exception type rescues all StandardErrors
    @ordertemps = {
      error: {
        status: 422,
        message: exception
      }
    }
  end

  render json: @ordertemp
     
end

And in your params method:

private

def ordertemp_params
  params.permit(:name, :amount, :price)
end
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot @Beartech, your code solved my situation, also, thanks a lot for the recommendations how to write properly my questions, I'll put them in practice for future ones. Best wishes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.