带回形针的Rails API [英] Rails api with paperclip

查看:67
本文介绍了带回形针的Rails API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有简单回形针模型的Rails api:

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

我的前端框架send像这样

My frontend framework send get like this

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

但这是错误的,因为rails等待跟随

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

推荐答案

如果您的服务器有一个看起来像这样的传入请求:

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"=>#...}}

两者之间的唯一区别是,首先, title 键位于 photo 嵌套哈希之外.但您希望它在里面.

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.

因此在您的Rails控制器中.你可以这样写:

So in your Rails controller,. you could write:

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

这篇关于带回形针的Rails API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆