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))