我正在尝试获取一个 json View ,该 View 使用 rabl 来让请求发送自定义属性,例如,如果模型有
:id, :first_name, :last_name
我只想调用我想要的值,比如只说 id 和 first_name,所以我不知道如何将字符串之类的东西转换为 rabble 必须返回的属性。
谢谢
最佳答案
首先在请求设置的 Controller 中创建变量。 例如
@attributes_choose_by_user = params[:attrs]
他们只是将该变量作为参数传递给查看器中的属性
# app/views/posts/index.rabl
collection @posts
attributes @attributes_choose_by_user
请求应该是这样的
GET /posts?attrs[]=id&attrs[]=first_name&attrs[]=last_name
关于ruby-on-rails - 动态属性列表 rabl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13906076/