MZokov
MZokov

Reputation: 326

Received JSON in Rails API is not stored

I am working on a web service in Rails that receives data in JSON format from another Rails application. When the web service receives the data, it reads it but does not insert into the database. This is from the log file:

Processing by OrdersController#create as JSON Parameters: {"name"=>"dst", "address"=>"dst", "email"=>"sdt"} [1m[36m (0.2ms)[0m [1mbegin transaction[0m [1m[35mSQL (0.5ms)[0m INSERT INTO "orders" ("created_at", "updated_at")

What should I do to get this data inside the INSERT statement? Do I need a new json.jbuilder file or something else? What am I missing?

Update: Putting the controller#create code here. Didn't paste it initially because it is the default created from the scaffold generator.

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)
      respond_to do |format|
        if @order.save
          format.html { redirect_to @order, notice: 'Order was successfully       created.' }
          format.json { render :show, status: :created, location: @order }
        else
          format.html { render :new }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
  end

Upvotes: 0

Views: 57

Answers (1)

Sean Marzug-McCarthy
Sean Marzug-McCarthy

Reputation: 86

order_params is presumably defined somewhere in your OrdersController. Normally model parameters in a request are nested under the resource ({order: {"name"=>"dst", "address"=>"dst", "email"=>"sdt"}) and then in order_params would be

def order_params
    params.require(:order).permit(:name, :address, :email)
end

But if you can't change the JSON payload, you could just nix the require(:order) part and call params.permit(:name, :address, :email).

You can read the Rails guide on Strong Parameters here: http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

Upvotes: 1

Related Questions