小编chi*_*ram的帖子

如何使用Rails Jbuilder提取所有属性?

在jbuilder.json模板中一直编写这样的代码是一件痛苦的事:

json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at
Run Code Online (Sandbox Code Playgroud)

所以我想像这样编码;

json.extract_all! notification
Run Code Online (Sandbox Code Playgroud)

我发现我可以像下面的代码那样做,但它们对我来说仍然有点冗长.

notification.attributes.each do |key, value|
  json.set!(key, value)
end
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法?

ruby-on-rails jbuilder

25
推荐指数
2
解决办法
1万
查看次数

如何在Rails中获取"应用程序跟踪"而不是完整的详细回溯?

在开发环境的Rails默认错误页面中,您可以查看三个回溯,1.应用程序跟踪,2.框架跟踪和3.完整跟踪.

但是如何在Rails控制器中获得"应用程序跟踪".完全跟踪(exception.backtrace)对我来说太过分了.现在我这样做:

exception.backtrace.select {|line| line =~ /myappname/i }
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?还是有其他更酷的方法来获得它?

exception-handling ruby-on-rails

8
推荐指数
1
解决办法
2058
查看次数

如何为 rails 中的特定 URL 设置超时

我使用机架超时,它工作正常。但我不知道如何为特定 URL 设置时间。

即使我喜欢:

映射 '/foo/bar' 做
  机架::超时.超时 = 10
结尾

不仅 /foo/bar 动作,而且每个动作都会在 10 秒后消失。

是否可以为特定 URL 设置超时?或者我应该使用机架超时以外的其他解决方案?

rack timeout ruby-on-rails

4
推荐指数
1
解决办法
3566
查看次数

命名空间内的浅路由:path param不起作用

此路线设置

namespace :api, path: nil, except: [:new, :edit] do
  resources :blogs do
    resources :comments
  end
end
Run Code Online (Sandbox Code Playgroud)

给了我这个,没关系.

GET    /blogs/:blog_id/comments(.:format)     api/comments#index
POST   /blogs/:blog_id/comments(.:format)     api/comments#create
GET    /blogs/:blog_id/comments/:id(.:format) api/comments#show
PATCH  /blogs/:blog_id/comments/:id(.:format) api/comments#update
DELETE /blogs/:blog_id/comments/:id(.:format) api/comments#destroy

GET    /blogs(.:format)                       api/blogs#index
POST   /blogs(.:format)                       api/blogs#create
GET    /blogs/:id(.:format)                   api/blogs#show
PATCH  /blogs/:id(.:format)                   api/blogs#update
DELETE /blogs/:id(.:format)                   api/blogs#destroy
Run Code Online (Sandbox Code Playgroud)

但是当我在上面的设置中添加"shallow:true"时

namespace :api, path: nil, except: [:new, :edit] do
  resources :blogs, shallow: true do
    resources :comments
  end
end
Run Code Online (Sandbox Code Playgroud)

一个不需要的路径'/ api'出现了.

/api/blogs/:blog_id/comments(.:format) api/comments#index
/api/blogs/:blog_id/comments(.:format) api/comments#create
/api/comments/:id(.:format)            api/comments#show
/api/comments/:id(.:format)            api/comments#update
/api/comments/:id(.:format)            api/comments#destroy

/blogs(.:format)                       api/blogs#index
/blogs(.:format) …
Run Code Online (Sandbox Code Playgroud)

routes ruby-on-rails

2
推荐指数
1
解决办法
1036
查看次数