Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails g scaffold for module resource generates wrong directory in jbuilder templates. #379

Open
rafaelfranca opened this issue Jan 30, 2017 · 3 comments

Comments

@rafaelfranca
Copy link
Member

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_at
    json.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_at
    json.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

@nullset2
Copy link

nullset2 commented Feb 5, 2017

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"
    }
]

@nullset2
Copy link

nullset2 commented Feb 6, 2017

Done, let me know if this works for you guys 😄

nullset2 pushed a commit to nullset2/jbuilder that referenced this issue Feb 6, 2017
@jung-hunsoo
Copy link

Looks fine, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants