How to display the error messages from the controller

I have a wizard which consists of two pages. To keep it simple let’s say
that the first page asks the user to select a state from the drop down
menu.
In case the user doesn’t select any state, an error message needs to be
displayed.

Ideally I would like to do something like this.

def new
selected_value = params[:addevent][:state_id]
if selected_value == “-1”
errors.add(:state_id,“Please select a valid state”)
else
# go to the next page
end
end

But errors is not available in the controller. It’s available only in
the
model. I could use flash[:notice] but using errors would have have
helped.
It would have kept everything consistent with the other error message.

Anything I’m missing here.

Thanks.

On Jan 11, 2006, at 9:56 AM, Neeraj K. wrote:

  errors.add(:state_id,"Please select a valid state")

I’m not sure I understand why your controller has to add the error
message. Does state_id belong to a model, or is it kind of
“floating” as unrelated data?

If it belongs to a model, I would do the check inside your model by
overriding the “validate” method.

e.g.

class User < ActiveRecord::Base
def validate
if state_id == -1
errors.add :state_id, “isn’t valid”
end
end
end

If it’s floating data, you can either attach it to a model:

class User < ActiveRecord::Base
attr_accessor :state_id #not in the database, but connected with
the User model
end

or you can handle it separately by using the flash, as you suggested.

Duane J.
(canadaduane)
http://blog.inquirylabs.com/

Thanks Duane.

As I mentioned it’s a wizard. The state_id is related to the database
but it
will be saved only when the rest of the data comes from the page2.

One the page1, the user has to choose a state that’s it and hit enter. I
need to verify if a state has been selected or not. I could use flash
here
but then it’s difficult to get the same look-n-feel. For example:

  1. Displaying messages like (one error prevented the submission of
    form).
  2. A state must be selected.

Having the same look-n-feel is difficult with flash and hence I was
looking
for an easier way.

-=- Neeraj

On 1/11/06, Duane J. [email protected] wrote:

def new
in the model. I could use flash[:notice] but using errors would

e.g.

class User < ActiveRecord::Base
def validate
if state_id == -1
errors.add :state_id, “isn’t valid”
end
end
end

If you override the validate method, does all the other automagical
validation in the model go away, or does this just add to it?

Thanks,

  • Ian

I haven’t tried it yet. Will give it a shot tonight.

The problem is that the model will have validations for page2 too and I
don’t want page2 stuff to be validated while performing the operation
for
page1.

I guess one solution could be to use a dummy model just for page1. Not
very
appealing but it might work and each alternative has it’s own kinks.

-=- Neeraj

On Jan 11, 2006, at 11:45 AM, Ian H. wrote:

 end

end
end

If you override the validate method, does all the other automagical
validation in the model go away, or does this just add to it?

Adds to it.

Duane J.
(canadaduane)
http://blog.inquirylabs.com/

On Jan 11, 2006, at 11:51 AM, Neeraj K. wrote:

-=- Neeraj
You can include variables in your model that you use to determine its
state. Later, you can check those variables in order to know what
should be validated.

For example:

class User < ActiveRecord::Base
attr_writer :steps_completed

def steps_completed
@steps_completed || 0
end

def validate
if steps_completed > 0
# … validate step 1 …
end

 if steps_completed > 1
   # ... validate step 2 ...
 end

end
end

You can also use conditional validations if you’re using the shortcut
validators:

class User < ActiveRecord::Base
attr_writer :steps_completed

def steps_completed
@steps_completed || 0
end

def after_step_one
@steps_completed > 1
end

validates_presence_of :state_id, :if => :after_step_one
end

Duane J.
(canadaduane)
http://blog.inquirylabs.com/