jbuilder

JBuilder dynamic keys for model attributes

余生颓废 提交于 2019-11-30 06:37:10
I'm trying to build a JSON file to access a product's variation via a variation code. Ideally, I could call variations.abcdefgh essentially asking to define a structure like this: { "variations":{ "abcdefgh":{ "available":true, "price":"12.00" }, "ijklmnop":{ "available":false, "price":"25.00" } } } Doing something in products/show.json.jbuilder like json.variations @product.variations gives me formatting as such: { "variations":[ { "available":true, "price":"12.00", "product_code":"abcdefgh" }, { "available":true, "price":"25.00", "product_code":"ijklmnop" } ] } maybe my JavaScript could be

JBuilder dynamic keys for model attributes

倖福魔咒の 提交于 2019-11-29 06:16:15
问题 I'm trying to build a JSON file to access a product's variation via a variation code. Ideally, I could call variations.abcdefgh essentially asking to define a structure like this: { "variations":{ "abcdefgh":{ "available":true, "price":"12.00" }, "ijklmnop":{ "available":false, "price":"25.00" } } } Doing something in products/show.json.jbuilder like json.variations @product.variations gives me formatting as such: { "variations":[ { "available":true, "price":"12.00", "product_code":"abcdefgh"

How could I render to a string a JSON representation of a JBuilder view?

て烟熏妆下的殇ゞ 提交于 2019-11-28 21:15:21
I'm using JBuilder as to return some JSON. I have a index.json.jbuilder that generates the data, and I need to render it to a string. However, I'm not sure how to do this, since: @my_object.to_json and @my_object.as_json don't seem to go through JBuilder. How could I render the JBuilder view as a string? I am rendering a collection of users as a json string in the controller like so: #controllers/users_controller.rb def index @users = User.all @users_json = render_to_string( template: 'users.json.jbuilder', locals: { users: @users}) end #views/users/users.json.jbuilder json.array!(users) do

How to extract all attributes with Rails Jbuilder?

二次信任 提交于 2019-11-28 11:53:28
It's a pain to write codes like this all the time in jbuilder.json template: json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at So I'd like to code like this; json.extract_all! notification I've found I can do it like the following codes, but they are still a bit lengthy to me. notification.attributes.each do |key, value| json.set!(key, value) end Is there any better way? Maybe you can use json.merge! . json.merge! notification.attributes https://github.com/rails/jbuilder/blob/master/lib/jbuilder.rb#L277 I'm using

Using JBuilder to create nested JSON output in rails

孤者浪人 提交于 2019-11-28 11:34:04
I am looking for examples on how to create nested JSON output using JBuilder. I want to create and output similar to this: { "name": "John Doe", "reservations": [ { "restaurant": "ABC", "reservation_time": "2012/12/01 20:00", "details": { "address": "somewhere", "rating": "5" } }, { "restaurant": "CDE", "reservation_time": "2012/12/04 20:00", "details": { "address": "somewhere else", "rating": "3" } } ] } Solved: json.name user.name json.array!(@reservations) do |json, reservation| json.restaurant reservation.restaurant.name json.reservation_time reservation.time json.details do json.address

使用了eclipse10年之后,我终于投向了IDEA

北城以北 提交于 2019-11-27 20:04:55
最近 ,改用了 IDEA ,同事都说我投敌了。当然,这些同事都是和我一样的“老”程序员。不说毕业生,公司里的 90 后基本电脑都不会安装 Eclipse。 Eclipse 的三足鼎立时期 开始我写 Java 程序,用的并不是 Eclipse,而是用的一款叫做 JBuilder 的工具。当时使用这个工具的时候,我已经觉得非常好用了,因为在此之前,我记得好像只是用个简单的文本工具来编辑。后来才发现,那些喜欢宣扬文本编辑器才能精通 Java 的“高手”真的有点不敢恭维,至少于我而言,一个好的工具太重要了。 并没有过了多久,我发现一款叫做 Eclipse 的工具, 而且身边用的人还越来越多,于是我决定试一试。尝试的结果就是我发现它实在是太好用了,界面变得更漂亮之余,里面的功能感觉就是为程序员量身定做的一样,最激动人心的功能是他竟然支持源源不断的插件,而且这个插件还可以自己来编写。 Eclipse 2001 年由 IBM 创立,2003 年就成立了基金会,可惜国内在那个时代总归要慢一步,而我则更落后——直到 07、08 年才开始真正使用。《程序员》在 2006 年还出了一期专题——Eclipse 风暴。 那时,Eclipse、JBuilder、NetBean 三足鼎立的局面已经基本形成了。 Eclipse 称霸 再后来出现了更加厉害的 MyEclipse,它集成了 所有常用插件,

How to extract all attributes with Rails Jbuilder?

我的未来我决定 提交于 2019-11-27 06:34:41
问题 It's a pain to write codes like this all the time in jbuilder.json template: json.extract! notification, :id, :user_id, :notice_type, :message, :resource_type, :resource_id, :unread, :created_at, :updated_at So I'd like to code like this; json.extract_all! notification I've found I can do it like the following codes, but they are still a bit lengthy to me. notification.attributes.each do |key, value| json.set!(key, value) end Is there any better way? 回答1: Maybe you can use json.merge! . json