mirror of
https://github.com/projekteuler/projekteuler.git
synced 2025-12-10 08:46:41 +01:00
Add controller for Problems with index and show actions
This commit is contained in:
parent
d1470f391e
commit
06ab2e4cab
3
app/assets/javascripts/problems.js.coffee
Normal file
3
app/assets/javascripts/problems.js.coffee
Normal file
@ -0,0 +1,3 @@
|
||||
# Place all the behaviors and hooks related to the matching controller here.
|
||||
# All this logic will automatically be available in application.js.
|
||||
# You can use CoffeeScript in this file: http://coffeescript.org/
|
||||
3
app/assets/stylesheets/problems.css.scss
Normal file
3
app/assets/stylesheets/problems.css.scss
Normal file
@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the problems controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
||||
21
app/controllers/problems_controller.rb
Normal file
21
app/controllers/problems_controller.rb
Normal file
@ -0,0 +1,21 @@
|
||||
class ProblemsController < ApplicationController
|
||||
before_action :set_problem, only: [:show]
|
||||
|
||||
def index
|
||||
@problems = Problem.paginate(page: params[:page])
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_problem
|
||||
@problem = Problem.find(params[:id])
|
||||
end
|
||||
|
||||
# Never trust parameters from the scary internet, only allow the white list through.
|
||||
def problem_params
|
||||
params.require(:problem)
|
||||
end
|
||||
end
|
||||
2
app/helpers/problems_helper.rb
Normal file
2
app/helpers/problems_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module ProblemsHelper
|
||||
end
|
||||
1
app/views/problems/_problem_pagination.erb
Normal file
1
app/views/problems/_problem_pagination.erb
Normal file
@ -0,0 +1 @@
|
||||
<%= will_paginate @problems, renderer: BootstrapPagination::Rails %>
|
||||
22
app/views/problems/index.html.erb
Normal file
22
app/views/problems/index.html.erb
Normal file
@ -0,0 +1,22 @@
|
||||
<h1>Listing Problems</h1>
|
||||
|
||||
<%= render 'problem_pagination' %>
|
||||
<table class="table table-striped table-bordered">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Title</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
<% @problems.each do |problem| %>
|
||||
<tr>
|
||||
<td><%= problem.id %></td>
|
||||
<td><%= link_to problem.title, problem %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<%= render 'problem_pagination' %>
|
||||
<br>
|
||||
16
app/views/problems/show.html.erb
Normal file
16
app/views/problems/show.html.erb
Normal file
@ -0,0 +1,16 @@
|
||||
<% if notice %>
|
||||
<p id="notice"><%= notice %></p>
|
||||
<% end %>
|
||||
|
||||
<div class="page-header">
|
||||
<h1><%= @problem.title %> <small>Problem <%= @problem.id %></small></h1>
|
||||
</div>
|
||||
|
||||
<%= panel do %>
|
||||
<div class="panel-body problem-content">
|
||||
<%= sanitize @problem.content %>
|
||||
</div>
|
||||
<% end %>
|
||||
<div class="text-center">
|
||||
<%= link_to 'Dieses Problem auf projecteuler.net', @problem.translation.original_url, target: '_blank' %>
|
||||
</div>
|
||||
@ -1,4 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :problems, only: [:index, :show]
|
||||
resources :translations
|
||||
|
||||
# The priority is based upon order of creation: first created -> highest priority.
|
||||
|
||||
@ -6,9 +6,14 @@
|
||||
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
|
||||
# Mayor.create(name: 'Emanuel', city: cities.first)
|
||||
|
||||
Translation.delete_all
|
||||
Problem.delete_all
|
||||
|
||||
for i in 1..103 do
|
||||
Translation.create(
|
||||
translation = Translation.create(
|
||||
problem_id: i,
|
||||
title: "Problem Nummer #{i}",
|
||||
content: "Das hier ist der Inhalt von <b>Problem #{i}</b>.<br />Hier ist die zweite Zeile."
|
||||
)
|
||||
Problem.create(id: i, translation: translation)
|
||||
end
|
||||
14
test/controllers/problems_controller_test.rb
Normal file
14
test/controllers/problems_controller_test.rb
Normal file
@ -0,0 +1,14 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ProblemsControllerTest < ActionController::TestCase
|
||||
test "should get index" do
|
||||
get :index
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should get show" do
|
||||
get :show, id: 1
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
end
|
||||
4
test/helpers/problems_helper_test.rb
Normal file
4
test/helpers/problems_helper_test.rb
Normal file
@ -0,0 +1,4 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ProblemsHelperTest < ActionView::TestCase
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user