Multiple rescue for create

I have this code below, it still gives me the error,
ActiveRecord::MultiparameterAssignmentErrors. It is not catching the
rescue
for some reason. Ideas?

def create
bad =[]
@tour_type = TourType.new(params[:tour_type])
if @tour_type.save
render :update do |page|
page << “Redbox.close;”
page.call “ProtoGrowl.success”, “Succesfully created
#{@tour_type.name}”
end
end
rescue ActiveRecord::MultiparameterAssignmentErrors
if params[:tour_type][:usual_price_number].blank?
bad << “Usual price must not be 0.00”
end

rescue ActiveRecord::RecordInvalid
 bad << "#{@tour_type.errors.full_messages}"

unless bad.empty?
 flash.now[:error] = "#{bad.length} Errors have occured in this 

form"
flash.now[:items] = bad
render :update do |page|
page[:flash].replace_html :partial => ‘shared/flash_box’ and
return
end
end
end

That is because you did not setup your error handling correctly. A
rescue
block is done within a begin and end. You don’t have that. You have
rescues
just tossed into your code. The format should look something like this:

begin
puts 10/0 #Bad code which will throw a ZeroDivisionError
rescue ZeroDivisionError
puts “Stop dividing by zero or the universe will end!”
end

See the docs on error handling for more information.

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_exceptions.html

Thanks,
B.

So is this the correct way:

def create
bad =[]
begin
@tour_type = TourType.new(params[:tour_type])
if @tour_type.save
render :update do |page|
page << “Redbox.close;”
page.call “ProtoGrowl.success”, “Succesfully created
#{@tour_type.name}”
end
end
rescue ActiveRecord::MultiparameterAssignmentErrors
if params[:tour_type][:usual_price_number].blank?
bad << “Usual price must not be 0.00”
end

  rescue ActiveRecord::RecordInvalid
   bad << "#{@tour_type.errors.full_messages}"

end
unless bad.empty?
 flash.now[:error] = "#{bad.length} Errors have occured in this 

form"
flash.now[:items] = bad
render :update do |page|
page[:flash].replace_html :partial => ‘shared/flash_box’ and
return
end
end
end

It is only catching the first rescue one when I know other validations
are
failing.

It looks fine but you have to run it to make sure it catches what you
want
it too.

B.

Because that is the top most error. The others are ignored until that
error
is fixed.

What are you trying to achieve with this error handling? It looks like
you
are looking to do validation on the information that is passed in. This
code
also looks like it is your controller. Is that correct?

B.

Yes it is in the controller, I will move it when I get it working. How
do I
get validations on both?

Ya I know, but I am getting this,
ActiveRecord::MultiparameterAssignmentErrors (1 error(s) on assignment
of
multiparameter attributes): with just the TourType.new for my time
select,
which is totally separate error from the validations.

On Fri, Feb 25, 2011 at 11:05 PM, Me [email protected] wrote:

Ya I know, but I am getting this,
ActiveRecord::MultiparameterAssignmentErrors (1 error(s) on assignment of
multiparameter attributes): with just the TourType.new for my time select,
which is totally separate error from the validations.

Paste the full error output, your model, and your form from the view.
The
error is coming because you are assigning too many, too few, and/or the
incorrect type of assignments that the model expects for a new record.
Once
you show me the stuff I listed I can get a better idea of which it is.

You’re better off handling the validations in the Model. Since these are
all
ActiveRecord errors you are getting.

If you want to validate in the controller before passing to the model
you
have to write code that will check for each thing you are looking to
validate (is it there?, does it fit the format you want?, etc.) before
you
pass it to the model. This can be a lot of code and a hassle to
maintain. I
would highly recommend that you just add a validates_presence_of
:usual_price_number in your model and in your controller do something
like
the following to start:

def create
@tour_type = TourType.new(params[:tour_type])

respond_to do |format|
if @tour_type.save
flash[:success] = ‘Tour was successfully created.’
format.html {redirect_to(@tour_type)}
format.xml { render :xml => @tour_type, :status => :created,
:location => @tour_type }
else
format.html { render :action => “new” }
format.xml { render :xml => @tour_type.errors, :status =>
:unprocessable_entity }
end
end
end

You can customize the error messages for your validates_presence_of and
add
additional validation types. See Rails documentation for more info.

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

B.

The error is from the time select that rails provides if you have
validations on that.

Top part of the model:

class TourType < ActiveRecord::Base
acts_as_reportable
has_many :tours, :dependent => :destroy
belongs_to :image, :class_name => “TourTypeImage”
belongs_to :usual_tour_confirmation, :class_name =>
“TourConfirmation”
belongs_to :page

validates_presence_of :name, :usual_start_time, :usual_end_time,
:abbreviation, :usual_tour_confirmation, :usual_cutoff
validates_numericality_of :usual_price_number, :greater_than => 0.00
validates_numericality_of :usual_capacity
acts_as_list
validates_presence_of :page_id, :if => lambda{|tt|tt.active?}
validates_each :usual_start_time do |r, a, v|
r.errors.add(a, ‘must be before usual end time’) if r.usual_end_time
<=
v
end

end

Here is the full error:

ActiveRecord::MultiparameterAssignmentErrors (1 error(s) on assignment
of
multiparameter attributes):

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activerecord-2.2.2/lib/active_record/base.rb:2892:in
`execute_callstack_for_multiparameter_attributes’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activerecord-2.2.2/lib/active_record/base.rb:2853:in
`assign_multiparameter_attributes’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activerecord-2.2.2/lib/active_record/base.rb:2591:in
`attributes=’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activerecord-2.2.2/lib/active_record/base.rb:2283:in
initialize' /app/models/tour_type.rb:72:innew’
/app/models/tour_type.rb:72:in create_type' /app/controllers/admin/tour_types_controller.rb:26:increate’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253:in
`send’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/base.rb:1253:in
`perform_action_without_filters’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/filters.rb:617:in
`call_filters’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/filters.rb:610:in
`perform_action_without_benchmark’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:68:in
`perform_action_without_rescue’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/benchmark.rb:293:in
`measure’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/benchmarking.rb:68:in
`perform_action_without_rescue’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/rescue.rb:136:in
`perform_action_without_caching’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/caching/sql_cache.rb:13:in
`perform_action’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activerecord-2.2.2/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in
`cache’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activerecord-2.2.2/lib/active_record/query_cache.rb:8:in
`cache’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/caching/sql_cache.rb:12:in
`perform_action’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/base.rb:524:in
`send’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/base.rb:524:in
`process_without_filters’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/filters.rb:606:in
`process_without_session_management_support’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/session_management.rb:134:in
`sass_old_process’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/haml-2.2.20/lib/sass/plugin/rails.rb:20:in
`process_without_compass’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/chriseppstein-compass-0.8.17/lib/compass/app_integration/rails/action_controller.rb:7:in
`process_without_test’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/test_process.rb:18:in
`process’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/base.rb:392:in
`process’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:183:in
`handle_request’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:110:in
`dispatch_unlocked’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:123:in
`dispatch’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:122:in
`synchronize’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:122:in
`dispatch’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:132:in
`dispatch_cgi’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/dispatcher.rb:39:in
`dispatch’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/rails-2.2.2/lib/webrick_server.rb:103:in
`handle_dispatch’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/rails-2.2.2/lib/webrick_server.rb:74:in
`service’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/httpserver.rb:104:in
`service’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/httpserver.rb:65:in
`run’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:173:in
`start_thread’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:162:in
`start’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:162:in
`start_thread’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:95:in
`start’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:92:in
`each’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:92:in
`start’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:23:in
`start’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/1.8/webrick/server.rb:82:in
`start’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/rails-2.2.2/lib/webrick_server.rb:60:in
`dispatch’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/rails-2.2.2/lib/commands/servers/webrick.rb:66

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
`gem_original_require’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
`require’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:153:in
`require’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:521:in
`new_constants_in’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:153:in
`require’

/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/rails-2.2.2/lib/commands/server.rb:49

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
`gem_original_require’

/Users/chabgood/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:29:in
`require’
script/server:3

Rendered
/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/templates/rescues/_trace
(147.1ms)
[4;36;1mSale Columns (43.1ms) [0m [0;1mSHOW FIELDS FROM sales
[0m
Rendered
/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/templates/rescues/_request_and_response
(335.7ms)
Rendering
/Users/chabgood/.rvm/gems/[email protected]_seattle/gems/actionpack-2.2.2/lib/action_controller/templates/rescues/layout.erb
(internal_server_error)

@tour_type = TourType.new(params[:tour_type])

On Fri, Feb 25, 2011 at 11:43 PM, Me [email protected] wrote:

@tour_type = TourType.new(params[:tour_type])

That is in your TourType model on line 72?

On Fri, Feb 25, 2011 at 11:23 PM, Me [email protected] wrote:

The error is from the time select that rails provides if you have
validations on that.

So your time select is for :usual_start_time and :usual_end_time I take
it.
The time that is being selected, is it in a format that your database
will
accept as valid?

/app/models/tour_type.rb:72:in `new’

Also, what is on line 72 of the model?

yes

You’re trying to call a create inside of a create. That makes no sense.
In
the controller you attempt to create the object TourType by calling
TourType.new and then while that is going on your attempt to create it
again
from inside the model by calling TourType.new. It’s erroring out
(thankfully) because the model has no idea what params[:tour_type] is.
If it
did, you would have an infinite loop.

Ok, I have a custom call in my controller to call the model method:

Controller:

def create
bad = TourType.create_type(params)

unless bad.empty?
 flash.now[:error] = "#{bad.length} Errors have occured in this 

form"
flash.now[:items] = bad
render :update do |page|
page[:flash].replace_html :partial => ‘shared/flash_box’
end
end
end

Model method:

def self.create_type(params)
bad =[]
#begin
@tour_type = TourType.new(params[:tour_type])
=begin
if @tour_type.save
render :update do |page|
page << “Redbox.close;”
page.call “ProtoGrowl.success”, “Succesfully created
#{@tour_type.name}”
end
end
rescue ActiveRecord::MultiparameterAssignmentErrors
if params[:tour_type][:usual_price_number].blank?
bad << “Usual price must not be 0.00”
end

  rescue ActiveRecord::RecordInvalid
   bad << "#{@tour_type.errors.full_messages}"

end
return bad

=end
end

On Feb 26, 6:00am, Me [email protected] wrote:

if @tour_type.save

Do you realise that this will never raise ActiveRecord::RecordInvalid?
If the record is invalid, save just returns false (save! raises)

Fred

And I take it you are still getting the error? If that is the case, then
change your view from using a datetime_select to using a date_select.
You
maybe encountering the problem discussed here:
http://stackoverflow.com/questions/93277/what-does-activerecordmultiparameterassignmenterrors-mean

On Feb 26, 6:13am, Bryan C. [email protected] wrote:

And I take it you are still getting the error? If that is the case, then
change your view from using a datetime_select to using a date_select. You
maybe encountering the problem discussed
here:http://stackoverflow.com/questions/93277/what-does-activerecordmultip

It would also be worth showing us what the params in the log look like
and what the corresponding database field types look like.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs