Skip to content

Commit 7ab0e35

Browse files
authored
Merge pull request #536 from hahmed/ha/fix-missing-namespaces-in-url
Fix namespace issue when generating jbuilder views
2 parents a9db396 + f67dacb commit 7ab0e35

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

lib/generators/rails/jbuilder_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def attributes_list(attributes = attributes_names)
5454
def virtual_attributes
5555
attributes.select {|name| name.respond_to?(:virtual?) && name.virtual? }
5656
end
57+
58+
def partial_path_name
59+
[controller_file_path, singular_table_name].join("/")
60+
end
5761
end
5862
end
5963
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
json.array! @<%= plural_table_name %>, partial: "<%= plural_table_name %>/<%= singular_table_name %>", as: :<%= singular_table_name %>
1+
json.array! @<%= plural_table_name %>, partial: "<%= partial_path_name %>", as: :<%= singular_table_name %>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
json.partial! "<%= plural_table_name %>/<%= singular_table_name %>", <%= singular_table_name %>: @<%= singular_table_name %>
1+
json.partial! "<%= partial_path_name %>", <%= singular_table_name %>: @<%= singular_table_name %>

test/jbuilder_generator_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ class JbuilderGeneratorTest < Rails::Generators::TestCase
4444
end
4545
end
4646

47+
test 'namespaced views are generated correctly for index' do
48+
run_generator %w(Admin::Post --model-name=Post)
49+
50+
assert_file 'app/views/admin/posts/index.json.jbuilder' do |content|
51+
assert_match %r{json\.array! @posts, partial: "admin/posts/post", as: :post}, content
52+
end
53+
54+
assert_file 'app/views/admin/posts/show.json.jbuilder' do |content|
55+
assert_match %r{json\.partial! "admin/posts/post", post: @post}, content
56+
end
57+
end
58+
4759
if Rails::VERSION::MAJOR >= 6
4860
test 'handles virtual attributes' do
4961
run_generator %w(Message content:rich_text video:attachment photos:attachments)

0 commit comments

Comments
 (0)