Skip to content

Commit 3a1c266

Browse files
committed
contentsページをrenderする前にtemplateファイルが在るか無いか確認する
1 parent 43878c3 commit 3a1c266

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

app/controllers/contents_controller.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def index
1111

1212
def discussion
1313
@conference = Conference.find_by(abbr: params[:event])
14-
render("#{@conference.abbr}_discussion".to_sym)
14+
render_if_template_exists("#{@conference.abbr}_discussion")
1515
end
1616

1717
def kontest
@@ -20,12 +20,12 @@ def kontest
2020

2121
def hands_on
2222
@conference = Conference.find_by(abbr: params[:event])
23-
render("#{@conference.abbr}_hands_on".to_sym)
23+
render_if_template_exists("#{@conference.abbr}_hands_on")
2424
end
2525

2626
def job_board
2727
@conference = Conference.find_by(abbr: params[:event])
28-
render("#{@conference.abbr}_job_board".to_sym)
28+
render_if_template_exists("#{@conference.abbr}_job_board")
2929
end
3030

3131
def o11y
@@ -35,16 +35,24 @@ def o11y
3535

3636
def community_lt
3737
@conference = Conference.find_by(abbr: params[:event])
38-
render("contents/#{@conference.abbr}/community_lt")
38+
render_if_template_exists("contents/#{@conference.abbr}/community_lt")
3939
end
4040

4141
def yurucafe
4242
@conference = Conference.find_by(abbr: params[:event])
43-
render("contents/#{@conference.abbr}/yurucafe")
43+
render_if_template_exists("contents/#{@conference.abbr}/yurucafe")
4444
end
4545

4646
def stamprally
4747
@conference = Conference.find_by(abbr: params[:event])
48-
render("contents/#{@conference.abbr}/stamprally")
48+
render_if_template_exists("contents/#{@conference.abbr}/stamprally")
49+
end
50+
51+
private
52+
53+
def render_if_template_exists(template)
54+
raise NotFound unless lookup_context.template_exists?(template, [], false)
55+
56+
render(template)
4957
end
5058
end

spec/requests/contents_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,30 @@
9494
end
9595
end
9696
end
97+
98+
describe 'GET missing content pages' do
99+
before do
100+
create(:cndt2020)
101+
end
102+
103+
it 'returns 404 for hands-on without a template' do
104+
get '/cndt2020/hands-on'
105+
expect(response).to(have_http_status('404'))
106+
end
107+
108+
it 'returns 404 for community_lt without a template' do
109+
get '/cndt2020/community_lt'
110+
expect(response).to(have_http_status('404'))
111+
end
112+
113+
it 'returns 404 for yurucafe without a template' do
114+
get '/cndt2020/yurucafe'
115+
expect(response).to(have_http_status('404'))
116+
end
117+
118+
it 'returns 404 for stamprally without a template' do
119+
get '/cndt2020/stamprally'
120+
expect(response).to(have_http_status('404'))
121+
end
122+
end
97123
end

0 commit comments

Comments
 (0)