1
0
mirror of https://github.com/projekteuler/projekteuler.git synced 2025-12-10 08:46:41 +01:00

Make translations a nested resource for each problem

This commit is contained in:
Philipp Fischbeck 2014-12-30 17:12:50 +01:00
parent 37f4eefff2
commit b16a04973a
7 changed files with 18 additions and 14 deletions

View File

@ -1,5 +1,6 @@
class TranslationsController < ApplicationController class TranslationsController < ApplicationController
before_action :set_translation, only: :show before_action :set_translation, only: :show
before_action :set_problem, only: [:new, :create]
# GET /translations # GET /translations
# GET /translations.json # GET /translations.json
@ -14,13 +15,13 @@ class TranslationsController < ApplicationController
# GET /translations/new # GET /translations/new
def new def new
@translation = Translation.new @translation = @problem.translations.build
end end
# POST /translations # POST /translations
# POST /translations.json # POST /translations.json
def create def create
@translation = Translation.new(translation_params) @translation = @problem.translations.new(translation_params)
respond_to do |format| respond_to do |format|
if @translation.save if @translation.save
@ -41,6 +42,10 @@ class TranslationsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through. # Never trust parameters from the scary internet, only allow the white list through.
def translation_params def translation_params
params.require(:translation).permit(:title, :content, :problem_id) params.require(:translation).permit(:title, :content)
end
def set_problem
@problem = Problem.find(params[:problem_id])
end end
end end

View File

@ -1,7 +1,7 @@
class Translation < ActiveRecord::Base class Translation < ActiveRecord::Base
belongs_to :problem, inverse_of: :translations belongs_to :problem, inverse_of: :translations
validates :title, :content, :problem, presence: true validates :title, :content, :problem_id, presence: true
validates :title, uniqueness: true validates :title, uniqueness: true
self.per_page = 50 self.per_page = 50

View File

@ -1,4 +1,4 @@
<%= form_for(@translation) do |f| %> <%= form_for([@problem, @translation]) do |f| %>
<% if @translation.errors.any? %> <% if @translation.errors.any? %>
<div id="error_explanation"> <div id="error_explanation">
<h2><%= pluralize(@translation.errors.count, "error") %> prohibited this problem from being saved:</h2> <h2><%= pluralize(@translation.errors.count, "error") %> prohibited this problem from being saved:</h2>

View File

@ -19,6 +19,4 @@
</tbody> </tbody>
</table> </table>
<%= render 'translation_pagination' %> <%= render 'translation_pagination' %>
<br> <br>
<%= link_to 'New Translation', new_translation_path, class: 'btn btn-default' %>

View File

@ -1,4 +1,4 @@
<h1>New translation</h1> <h1>New translation for problem <%= @problem.id %></h1>
<%= render 'form' %> <%= render 'form' %>

View File

@ -1,6 +1,8 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :problems, only: [:index, :show] resources :problems, only: [:index, :show] do
resources :translations, only: [:index, :show, :new, :create] resources :translations, only: [:index, :new, :create]
end
resources :translations, only: [:index, :show]
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes". # See how all your routes lay out with "rake routes".

View File

@ -6,7 +6,6 @@ class TranslationsControllerTest < ActionController::TestCase
@update = { @update = {
title: 'New title', title: 'New title',
content: 'This is the new content', content: 'This is the new content',
problem_id: 1
} }
end end
@ -17,13 +16,13 @@ class TranslationsControllerTest < ActionController::TestCase
end end
test "should get new" do test "should get new" do
get :new get :new, problem_id: 1
assert_response :success assert_response :success
end end
test "should create translation" do test "should create translation" do
assert_difference('Translation.count') do assert_difference('Translation.count') do
post :create, translation: @update post :create, problem_id: 1, translation: @update
end end
assert_redirected_to translation_path(assigns(:translation)) assert_redirected_to translation_path(assigns(:translation))