jbuilder
jbuilder copied to clipboard
Rails g scaffold for module resource generates wrong directory in jbuilder templates.
From @jung-hunsoo on January 30, 2017 10:9
Steps to reproduce
$ rails g scaffold guest/category title description
Expected behavior
While many new files being generated, three jbuilder templates are expected to be created like this:
-
_guest_category.json.jbuilder
json.extract! guest_category, :id, :title, :description, :created_at, :updated_atjson.url guest_category_url(guest_category, format: :json) -
index.json.jbuilder
json.array! @guest_categories, partial: 'guest/categories/guest_category', as: :guest_category -
show.json.jbuilder
json.partial! "guest/categories/guest_category", guest_category: @guest_category
Actual behavior
The two jbuilder templates for index and show have inexistent directory which the module namespace isn't applied.
-
_guest_category.json.jbuilder
json.extract! guest_category, :id, :title, :description, :created_at, :updated_atjson.url guest_category_url(guest_category, format: :json) -
index.json.jbuilder
json.array! @guest_categories, partial: 'guest_categories/guest_category', as: :guest_category -
show.json.jbuilder
json.partial! "guest_categories/guest_category", guest_category: @guest_category
System configuration
Rails version: 5.0.1
Ruby version: 2.3.3
Copied from original issue: rails/rails#27845
I just ran into this, looking to submit test + patches soon.
System configuration:
Rails version: 5.0.1
Ruby version: 2.3.1
Basically, using jbuilder from the rails point of view (rails 5.0.0 in api-mode afaik uses jbuilder by default, and in 5.0.0.1 you can enable it after you create the project) when invoking the rails generate scaffold command to create a resource under a namespace, view code is not correctly generated.
For example in my index.json.jbuilder which I got from a rails g scaffold api/oof rab:integer zab:string on a rails app that uses jbuilder I got out of the box as the file describing the index for all oofs app/views/api/oofs/index.json.jbuilder:
json.array! @api_oofs, partial: 'api_oofs/api_oof', as: :api_oof
...which breaks the app, since, for example, the partial really is in api/oofs/api_oof.json.jbuilder, not in api_oofs/api_oof.json, which is what the framework is assuming:
http localhost:3000/api/oofs
HTTP/1.1 500 Internal Server Error
Content-Length: 23193
Content-Type: application/json; charset=utf-8
X-Request-Id: 1f7969bf-d191-43a5-879d-c7b9fd01602d
X-Runtime: 0.124838
{
"error": "Internal Server Error",
"exception": "#<ActionView::Template::Error: Missing partial api_oofs/_api_oof with {:locale=>[:en], :formats=>[:json, :html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:jbuilder]}. Searched in:\n * \"/Users/alfredo/Documents/workspace/testy/app/views\"\n>",
"status": 500,
"traces": {
..........
This also affects the other locations that @rafaelfranca mentioned.
Changing the view code from api_oofs/api_oof.json to api/oofs/api_oof.json fixes the problem:
http localhost:3000/api/oofs
HTTP/1.1 200 OK
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8
ETag: W/"df0e20288f645cecb9e995c96977aec7"
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: caded523-f9c5-4c08-82d0-1dc977e9db74
X-Runtime: 0.027929
X-XSS-Protection: 1; mode=block
[
{
"created_at": "2017-02-05T05:09:31.332Z",
"id": 1,
"rab": 1,
"updated_at": "2017-02-05T05:09:31.332Z",
"url": "http://localhost:3000/api/oofs/1.json",
"zab": "hello"
},
{
"created_at": "2017-02-05T05:09:33.632Z",
"id": 2,
"rab": 1,
"updated_at": "2017-02-05T05:09:33.632Z",
"url": "http://localhost:3000/api/oofs/2.json",
"zab": "hello"
}
]
Done, let me know if this works for you guys 😄
Looks fine, thanks.