RailsCasts Pro episodes are now free!

Learn more or hide this

Asang Dani's Profile

GitHub User: asang

Site: http://quest.ksetindia.com/

Comments by Asang Dani

Avatar

If you are still interested, you can checkout my forked repo on github - https://github.com/asang/403-dynamic-forms

Avatar

In order for this to work with Rails 4.0 or later, you have to use http://blog.trackets.com/2013/08/17/strong-parameters-by-example.html and make following changes to app/controllers/survey_controller.rb

--- questionnaire-after/app/controllers/surveys_controller.rb   2012-04-19 10:45:53.000000000 +0530
+++ /home/asang/questionnaire-after/app/controllers/surveys_controller.rb       2015-04-05 19:13:56.746426275 +0530
@@ -12,7 +12,7 @@
   end
 
   def create
-    @survey = Survey.new(params[:survey])
+    @survey = Survey.new(survey_params)
     if @survey.save
       redirect_to @survey, notice: "Successfully created survey."
     else
@@ -26,7 +26,7 @@
 
   def update
     @survey = Survey.find(params[:id])
-    if @survey.update_attributes(params[:survey])
+    if @survey.update_attributes(survey_params)
       redirect_to @survey, notice: "Successfully updated survey."
     else
       render :edit
@@ -38,4 +38,12 @@
     @survey.destroy
     redirect_to surveys_url, notice: "Successfully destroyed survey."
   end
+
+  private
+
+  def survey_params
+       params.require(:survey).permit(:name).tap do |w|
+               w[:questions_attributes] = params[:survey][:questions_attributes]
+       end
+  end
 end
+++ /home/asang/questionnaire-after/app/models/answer.rb        2015-04-05 19:13:56.746426275 +0530
@@ -1,4 +1,3 @@
 class Answer < ActiveRecord::Base
-  attr_accessible :content, :question_id
   belongs_to :question
 end
diff -uwrN questionnaire-after/app/models/question.rb /home/asang/questionnaire-after/app/models/question.rb
--- questionnaire-after/app/models/question.rb  2012-04-19 10:45:53.000000000 +0530
+++ /home/asang/questionnaire-after/app/models/question.rb      2015-04-05 19:13:56.746426275 +0530
@@ -1,5 +1,4 @@
 class Question < ActiveRecord::Base
-  attr_accessible :content, :survey_id, :answers_attributes
   belongs_to :survey
   has_many :answers
   accepts_nested_attributes_for :answers, allow_destroy: true
diff -uwrN questionnaire-after/app/models/survey.rb /home/asang/questionnaire-after/app/models/survey.rb
--- questionnaire-after/app/models/survey.rb    2012-04-19 10:45:53.000000000 +0530
+++ /home/asang/questionnaire-after/app/models/survey.rb        2015-04-05 19:13:56.746426275 +0530
@@ -1,5 +1,4 @@
 class Survey < ActiveRecord::Base
-  attr_accessible :name, :questions_attributes
   has_many :questions
   accepts_nested_attributes_for :questions, allow_destroy: true
 end

Few other changes are required in GemFile

diff -uwrN questionnaire-after/Gemfile /home/asang/questionnaire-after/Gemfile
--- questionnaire-after/Gemfile 2012-04-19 10:45:53.000000000 +0530
+++ /home/asang/questionnaire-after/Gemfile     2015-04-05 19:13:56.751426282 +0530
@@ -1,6 +1,6 @@
 source 'https://rubygems.org'
 
-gem 'rails', '3.2.3'
+gem 'rails', '~> 4.2.1'
 
 # Bundle edge Rails instead:
 # gem 'rails', :git => 'git://github.com/rails/rails.git'
@@ -11,8 +11,8 @@
 # Gems used only for assets and not required
 # in production environments by default.
 group :assets do
-  gem 'sass-rails',   '~> 3.2.3'
-  gem 'coffee-rails', '~> 3.2.1'
+  gem 'sass-rails'
+  gem 'coffee-rails'

and some other files:

diff -uwrN questionnaire-after/config/application.rb /home/asang/questionnaire-after/config/application.rb
--- questionnaire-after/config/application.rb   2012-04-19 10:45:53.000000000 +0530
+++ /home/asang/questionnaire-after/config/application.rb       2015-04-05 19:13:56.739426266 +0530
@@ -48,7 +48,7 @@
     # This will create an empty whitelist of attributes available for mass-assignment for all models
     # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
     # parameters by using an attr_accessible or attr_protected declaration.
-    config.active_record.whitelist_attributes = true
+    #config.active_record.whitelist_attributes = true
 
     # Enable the asset pipeline
     config.assets.enabled = true
diff -uwrN questionnaire-after/config/environments/development.rb /home/asang/questionnaire-after/config/environments/development.rb
--- questionnaire-after/config/environments/development.rb      2012-04-19 10:45:53.000000000 +0530
+++ /home/asang/questionnaire-after/config/environments/development.rb  2015-04-05 19:13:56.739426266 +0530
@@ -23,11 +23,11 @@
   config.action_dispatch.best_standards_support = :builtin
 
   # Raise exception on mass assignment protection for Active Record models
-  config.active_record.mass_assignment_sanitizer = :strict
+  #config.active_record.mass_assignment_sanitizer = :strict
 
   # Log the query plan for queries taking more than this (works
   # with SQLite, MySQL, and PostgreSQL)
-  config.active_record.auto_explain_threshold_in_seconds = 0.5
+  #config.active_record.auto_explain_threshold_in_seconds = 0.5
 
   # Do not compress assets
   config.assets.compress = false