I'm using rails 4.0.1 and ruby 2.0.0 with devise 3.2.2. I keep getting this issue when a user tries to edit his account. I have my strong parameters in application_controller.rb and a custom update controller which looks like this:
class RegistrationsController < Devise::RegistrationsController
def update
# For Rails 4
account_update_params = devise_parameter_sanitizer.for(:account_update)
#user = User.find(current_user.id)
#user.balanced_associate_token_with_customer(account_update_params)
if #user.update_with_password(account_update_params)
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in #user, :bypass => true
redirect_to after_update_path_for(#user)
else
render "edit"
end
end
protected
def after_update_path_for(resource)
user_path(resource)
end
end
I have been pulling my hairs since yesterday...any assitance would be appreciated. Let me know if you need any more info.
Edited:
The only thing that changed before i noticed the error is that devise starting asking for config.secret_key in devise initialize file.
A portion of the full trace shows:
devise (3.2.2) lib/devise/models/database_authenticatable.rb:64:in `[]'
devise (3.2.2) lib/devise/models/database_authenticatable.rb:64:in `update_with_password'
app/controllers/registrations_controller.rb:12:in `update'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
Thanks!
I fixed the issue after a number of hours. Hopefully this helps someone out there. Seems Devise changed its documentation. Anyway I removed this line:
account_update_params = devise_parameter_sanitizer.for(:account_update)
and changed this line:
if #user.update_with_password(account_update_params)
to
if #user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
Everything works..now I need a drink.
Related
I have a complicated case where I'm developing a Rails 3 Engine and I only intermittently get the error in the title. Here's the stacktrace:
ActiveRecord::ConfigurationError - Association named 'whatever' was not found; perhaps you misspelled it?:
activerecord (3.2.18) lib/active_record/associations/preloader.rb:150:in `block in records_by_reflection'
activerecord (3.2.18) lib/active_record/associations/preloader.rb:146:in `records_by_reflection'
activerecord (3.2.18) lib/active_record/associations/preloader.rb:139:in `grouped_records'
activerecord (3.2.18) lib/active_record/associations/preloader.rb:130:in `preload_one'
activerecord (3.2.18) lib/active_record/associations/preloader.rb:109:in `preload'
activerecord (3.2.18) lib/active_record/associations/preloader.rb:98:in `block in run'
activerecord (3.2.18) lib/active_record/associations/preloader.rb:98:in `run'
activerecord (3.2.18) lib/active_record/relation.rb:181:in `block in exec_queries'
activerecord (3.2.18) lib/active_record/relation.rb:180:in `exec_queries'
activerecord (3.2.18) lib/active_record/relation.rb:160:in `block in to_a'
activerecord (3.2.18) lib/active_record/explain.rb:41:in `logging_query_plan'
activerecord (3.2.18) lib/active_record/relation.rb:159:in `to_a'
activerecord (3.2.18) lib/active_record/relation/delegation.rb:39:in `+'
/Users/me/src/appointment_engine/app/controllers/appointment_engine/appointments_controller.rb:42:in `block (3 levels) in index'
/Users/me/src/appointment_engine/app/controllers/appointment_engine/appointments_controller.rb:41:in `block (2 levels) in index'
actionpack (3.2.18) lib/action_controller/metal/mime_responds.rb:196:in `respond_to'
/Users/me/src/appointment_engine/app/controllers/appointment_engine/appointments_controller.rb:12:in `index'
To summarize: There's a model named Appointment in the engine which is polymorphically associated with has_many :through to the host app's User model (this is a requirement because we also associate to another model). Here's the has_many declaration in Appointment
class Appointment < ActiveRecord::Base
has_many :scheduleables,
through: :appointments_scheduleables,
source_type: KAE.scheduleable_class.name
has_many :schedulers,
through: :appointments_schedulers,
source_type: KAE.scheduler_class.name
end
Here I ran into my first problem; I need to set the :source_type on has_many :through polymorphic associations (It doesn't work without it) and for that I need to know the class of the associated model but, when my engine's Appointment model loads it does so before the host app's User model loads and therefore my engine's module KAE hasn't received the value for KAE.scheduleable_class yet.
Here's how KAE receives that value:
# in host app
class User < ActiveRecord::Base
acts_as_scheduler
end
I wrote acts_as_scheduler as an AR mixin, it will declare the has_many :through association to Appointment.
My first attempt to fix this: I put the Appointment's has_many declaration in a hook inside a railtie:
ActiveSupport.on_load :after_initialize do
KAE::Appointment.class_eval do
has_many :scheduleables,
through: :appointments_scheduleables,
source_type: KAE.scheduleable_class.name
has_many :schedulers,
through: :appointments_schedulers,
source_type: KAE.scheduler_class.name
end
end
Ok now that works, I wait till the host app loads completely and now I have the values for KAE.scheduleable_class and KAE.scheduler_class, great.
Except I get the error in the title intermittently!
I can boot up fine, use the app for a while (10-30 mins) and then out of nowhere boom! I've tried it under rails server, thin, and unicorn; all the same so it must be an app/framework level bug. I look in the active record preloader class to where the top of the stacktrace points to:
# lib/active_record/associations/preloader.rb:150
def records_by_reflection(association)
records.group_by do |record|
reflection = record.class.reflections[association]
unless reflection
raise ActiveRecord::ConfigurationError, "Association named '#{association}' was not found; " \
"perhaps you misspelled it?"
end
reflection
end
end
How can a model forget some of it's associations?
So now I'm doing this directly in the Appointment model and it seems to be working so far but it's really ugly:
class Appointment < ActiveRecord::Base
if KAE.scheduler_class && KAE.scheduleable_class
has_many :scheduleables,
through: :appointments_scheduleables,
source_type: KAE.scheduleable_class.name
has_many :schedulers,
through: :appointments_schedulers,
source_type: KAE.scheduler_class.name
else
binding.pry # TODO
ActiveSupport.on_load :after_initialize do
KAE::Appointment.class_eval do
has_many :scheduleables,
through: :appointments_scheduleables,
source_type: KAE.scheduleable_class.name
has_many :schedulers,
through: :appointments_schedulers,
source_type: KAE.scheduler_class.name
end
end
end
end
Anybody know of a better way of declaring has_many :through polymorphic associations in a Rails 3 Engine?
I've been looking at open source projects like acts_as_taggable_on_steroids and paper_trail to see how they do their associations but they don't have polymorphic has_many :through.
Anybody know any projects that have this type of association?
Load ordering can be a pain with Rails autoloading+auto-reloading magic. In these situations I just make my dependencies more explicit. I suggest you try using Rails's require_dependency in your host to ensure this doesn't occur:
# in host app
require_dependency 'appointment'
class User < ActiveRecord::Base
acts_as_scheduler
end
It's ActiveSupport's version of require that also plays well with the development environment's auto-reloading, which is probably why you're experiencing this issue intermittently when working on your application.
I'm using the will_paginate gem, and trying to implement DataTables, per this RailsCast, and discovered that it is a good idea to supply the attribute total_entries, because will_paginate apparently does not do this very well.
So I added this method to my class like so:
class Genotype < ActiveRecord::Base
attr_accessible :allele1, :allele2, :run_date
belongs_to :gmarkers
belongs_to :gsamples
def total_entries
#t = Genotype.count;
return #t
end
end
However, I still get this error when the view tries to load (this is a screenshot of the webserver responses):
(7.6ms) SELECT COUNT(*) FROM "genotypes"
Completed 500 Internal Server Error in 96ms
NoMethodError (undefined method `total_entries' for nil:NilClass):
app/datatables/genotypes_datatable.rb:13:in `as_json'
app/controllers/genotypes_controller.rb:7:in `block (2 levels) in index'
app/controllers/genotypes_controller.rb:5:in `index'
Obviously I must be doing something wrong in defining my method; but what? I am too inexperienced in Rails to stumble my way out of this...
Thanks in advance,
Rick
I am following along the tutorial Ruby on Rail 3 Essential Training from Lynda.com. I am having a difficult time creating an Active Record Entry. This is the error I get in my console.
1.9.3p125 :007 > user = User.new(:first_name => "Mike", :last_name => "Jones")
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: first_name, last_name
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_removal'
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security/sanitizer.rb:12:in `sanitize'
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.3/lib/active_model/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/attribute_assignment.rb:75:in `assign_attributes'
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.3/lib/active_record/base.rb:498:in `initialize'
from (irb):7:in `new'
from (irb):7
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /home/mark/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>`
This is what I have in my Model:
class User < ActiveRecord::Base
attr_accessible :first_name, :last_name
end
What am I doing wrong. I have rails 3.2.3
From what I know that lynda course was developed on rails3 and in rails 3.2.3 there is no mass assignment by default. You have to go your model and add attr_accessible :name, :position, :visible. Basically you have to add every attribute you want to mass assign.
Try to restart the console. If you have created the model for user after the console was launched, you should restart it.
Without any precautions Mass-assignment allows attackers to set any database column’s value, hence it has been disabled by default.
def signup
params[:user] # => {:name => “ow3ned”, :admin => true}
#user = User.new(params[:user])
end
The detailed description is in the Ruby On Rails Security Guide.
I just added the attr_accessible :first_name, :last_name, :username line to the models file.
This worked for me.
I was too following along the tutorial Ruby on Rail 3 Essential Training from Lynda.com, if anybody had the same problem here is what worked for me,
Turn off the security setting. Open config/application.rb and change config.active_record.whitelist_attributes to false instead of true. This makes your app a little less secure, but allows you to quickly move forward with the tutorial.
this is from: http://www.lynda.com/Ruby-on-Rails-3-tutorials/essential-training/55960-2/faqs
Make sure to put attr_accessible :first_name, :last_name in the User model and not in the controller.
Our platform has been online for a while and working just fine. Our customers are able to download their invoices in PDF format just fine.
We've been working on a upgrades for a few month, and just today we noticed that "suddenly" non of our PDF generation with wicked_pdf and wkhtmltopdf no longer worked...
I have absolutely no idea why, I checked everything I could think of:
- routes
- initializers
- gems
- etc.
Everything seems to be fine, same as the actual only version.
We have not changed Rails or Ruby version. Everything is pretty much the same:
- Ruby 1.8.7 REE
- Rails 3.0.10
The error we are getting is:
Rendered groups/clients/proforma.pdf.haml (103.4ms)
Sent data toto.pdf (2.7ms)
Completed 500 Internal Server Error in 6892ms
NoMethodError (undefined method `virtual_path' for text template:ActionView::Template::Text):
app/controllers/groups/clients_controller.rb:980:in `proforma'
app/controllers/groups/clients_controller.rb:976:in `proforma'
lib/include/flash_session_cookie_middleware.rb:16:in `call'
lib/include/flash_session_cookie_middleware.rb:16:in `call'
The controller looks like:
def proforma
#request = WireRequest.where(:_id => params[:id], :status => :pending).first
respond_to do |format|
format.html {render :layout => false}
format.pdf do
unless #request.nil?"
render(:pdf => "PROFORMA_#{#request.invoice_num}", :layout => 'pdf')
end
end
end
end
Any ideas of what could be going wrong? I have no more ideas :(
I already had this kind of issue using the new relic rpm for developers.
I forked the gem here.
I'm in the process of upgrading to Rails 3.1.0 from 3.0.10, and I'm using Devise for authentication.
I have a number of functional tests that require me to set a single session variable. Stuff along the lines of:
test "new clears current_lesson_id from session" do
get :new, nil, {'current_lesson_id' => '234234'}
assert_response :success
assert_nil session[:current_lesson_id]
end
This was failing as my session info was clobbering the devise authentication session data, so my previous solution was to merge with the default session:
test "new clears current_lesson_id from session" do
get :new, nil, authenticated_sesion_with({'current_lesson_id' => '234234'})
assert_response :success
assert_nil session[:current_lesson_id]
end
def authenticated_session_with(hash)
session.merge(hash)
end
All of this worked fine with rails 3.0.10 (with warden 1.0.4 and devise 1.4.2), but no longer with rails 3.1.0 (and warden 1.0.5, devise 1.4.4). Now I'm getting the following error:
NoMethodError: private method `stringify_keys' called for <ActionController::TestSession:0x000001060db590>
I gather this is because the 'session' object is an instance of ActionController::TestSession, and in rails 3.1.0 there are a bunch of instance variables that can't and shouldn't be 'stringified'.
How should I properly access the devise user information (preferably dynamically) so I can add 'current_lesson_id', etc.?
Thanks much,
try this to fix your error in Rails 3.1
sign_in user
hashy = session['warden.user.user.key'][2]
get :action, nil, {"warden.user.user.key"=>["User", [user.id],hashy]}, nil
Worked for me. Seems to not like the new way Rails handles TestSessions.
I think you better do:
def valid_session
my_session_values = {:some_key => :some_value}
session.to_hash.merge my_session_values
end