From b16a04973a1089450ae9a75acf64cef20236519a Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 17:12:50 +0100 Subject: [PATCH] Make translations a nested resource for each problem --- app/controllers/translations_controller.rb | 11 ++++++++--- app/models/translation.rb | 2 +- app/views/translations/_form.html.erb | 2 +- app/views/translations/index.html.erb | 4 +--- app/views/translations/new.html.erb | 2 +- config/routes.rb | 6 ++++-- test/controllers/translations_controller_test.rb | 5 ++--- 7 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 7d63d70..3134b7b 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -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 diff --git a/app/models/translation.rb b/app/models/translation.rb index 04227f5..8253535 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -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 diff --git a/app/views/translations/_form.html.erb b/app/views/translations/_form.html.erb index 9656465..19910fd 100644 --- a/app/views/translations/_form.html.erb +++ b/app/views/translations/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_for(@translation) do |f| %> +<%= form_for([@problem, @translation]) do |f| %> <% if @translation.errors.any? %>

<%= pluralize(@translation.errors.count, "error") %> prohibited this problem from being saved:

diff --git a/app/views/translations/index.html.erb b/app/views/translations/index.html.erb index 02764f1..ad98e8d 100644 --- a/app/views/translations/index.html.erb +++ b/app/views/translations/index.html.erb @@ -19,6 +19,4 @@ <%= render 'translation_pagination' %> -
- -<%= link_to 'New Translation', new_translation_path, class: 'btn btn-default' %> +
\ No newline at end of file diff --git a/app/views/translations/new.html.erb b/app/views/translations/new.html.erb index 45f1fd9..47224da 100644 --- a/app/views/translations/new.html.erb +++ b/app/views/translations/new.html.erb @@ -1,4 +1,4 @@ -

New translation

+

New translation for problem <%= @problem.id %>

<%= render 'form' %> diff --git a/config/routes.rb b/config/routes.rb index 756ec6d..a0f3fe3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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". diff --git a/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index 4314187..41b3e18 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -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))