Alexander Borovoi
Alexander Borovoi

Reputation: 124

Rails api with paperclip

I have rails api with simple paperclip model:

def create
@photo = Photo.new(photo_params)

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

private    

def photo_params
  params.require(:photo).permit(:image, :title)
end

My frontend framework send get like this

{"title"=>"simpletitletext", "photo"=>{"image"=>......}}

But it wrong, because rails waits following

{"photo"=>{"title"=>"simpletitle", "image"=>#...}}

I had been trying for different ways to fix angular for many hours, before wrote . May be it will be able to fix in rails

Upvotes: 2

Views: 202

Answers (1)

max pleaner
max pleaner

Reputation: 26778

If your server has an incoming request that looks like this:

{"title"=>"simpletitletext", "photo"=>{"image"=>......}}

you can make it look like this:

{"photo"=>{"title"=>"simpletitle", "image"=>#...}}

The only difference between the two is that in the first, the title key is outside of the photo nested hash. but you want it to be inside.

So in your Rails controller,. you could write:

def photo_params
  hash = params[:photo]
  hash.merge(title: params[:title])
end

Upvotes: 1

Related Questions