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:
parent
37f4eefff2
commit
b16a04973a
@ -1,5 +1,6 @@
|
||||
class TranslationsController < ApplicationController
|
||||
before_action :set_translation, only: :show
|
||||
before_action :set_problem, only: [:new, :create]
|
||||
|
||||
# GET /translations
|
||||
# GET /translations.json
|
||||
@ -14,13 +15,13 @@ class TranslationsController < ApplicationController
|
||||
|
||||
# GET /translations/new
|
||||
def new
|
||||
@translation = Translation.new
|
||||
@translation = @problem.translations.build
|
||||
end
|
||||
|
||||
# POST /translations
|
||||
# POST /translations.json
|
||||
def create
|
||||
@translation = Translation.new(translation_params)
|
||||
@translation = @problem.translations.new(translation_params)
|
||||
|
||||
respond_to do |format|
|
||||
if @translation.save
|
||||
@ -41,6 +42,10 @@ class TranslationsController < ApplicationController
|
||||
|
||||
# Never trust parameters from the scary internet, only allow the white list through.
|
||||
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
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
class Translation < ActiveRecord::Base
|
||||
belongs_to :problem, inverse_of: :translations
|
||||
|
||||
validates :title, :content, :problem, presence: true
|
||||
validates :title, :content, :problem_id, presence: true
|
||||
validates :title, uniqueness: true
|
||||
|
||||
self.per_page = 50
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<%= form_for(@translation) do |f| %>
|
||||
<%= form_for([@problem, @translation]) do |f| %>
|
||||
<% if @translation.errors.any? %>
|
||||
<div id="error_explanation">
|
||||
<h2><%= pluralize(@translation.errors.count, "error") %> prohibited this problem from being saved:</h2>
|
||||
|
||||
@ -20,5 +20,3 @@
|
||||
</table>
|
||||
<%= render 'translation_pagination' %>
|
||||
<br>
|
||||
|
||||
<%= link_to 'New Translation', new_translation_path, class: 'btn btn-default' %>
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<h1>New translation</h1>
|
||||
<h1>New translation for problem <%= @problem.id %></h1>
|
||||
|
||||
<%= render 'form' %>
|
||||
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :problems, only: [:index, :show]
|
||||
resources :translations, only: [:index, :show, :new, :create]
|
||||
resources :problems, only: [:index, :show] do
|
||||
resources :translations, only: [:index, :new, :create]
|
||||
end
|
||||
resources :translations, only: [:index, :show]
|
||||
|
||||
# The priority is based upon order of creation: first created -> highest priority.
|
||||
# See how all your routes lay out with "rake routes".
|
||||
|
||||
@ -6,7 +6,6 @@ class TranslationsControllerTest < ActionController::TestCase
|
||||
@update = {
|
||||
title: 'New title',
|
||||
content: 'This is the new content',
|
||||
problem_id: 1
|
||||
}
|
||||
end
|
||||
|
||||
@ -17,13 +16,13 @@ class TranslationsControllerTest < ActionController::TestCase
|
||||
end
|
||||
|
||||
test "should get new" do
|
||||
get :new
|
||||
get :new, problem_id: 1
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create translation" do
|
||||
assert_difference('Translation.count') do
|
||||
post :create, translation: @update
|
||||
post :create, problem_id: 1, translation: @update
|
||||
end
|
||||
|
||||
assert_redirected_to translation_path(assigns(:translation))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user