Forum: Ruby on Rails How to display the error messages from the controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Neeraj Kumar (Guest)
on 2006-01-11 17:57
(Received via mailing list)
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.
Duane Johnson (Guest)
on 2006-01-11 19:15
(Received via mailing list)
On Jan 11, 2006, at 9:56 AM, Neeraj Kumar 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 Johnson
(canadaduane)
http://blog.inquirylabs.com/
Neeraj Kumar (Guest)
on 2006-01-11 19:46
(Received via mailing list)
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
Ian Harding (Guest)
on 2006-01-11 19:46
(Received via mailing list)
On 1/11/06, Duane Johnson <duane.johnson@gmail.com> 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
Neeraj Kumar (Guest)
on 2006-01-11 19:52
(Received via mailing list)
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
Duane Johnson (Guest)
on 2006-01-11 21:14
(Received via mailing list)
On Jan 11, 2006, at 11:45 AM, Ian Harding 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 Johnson
(canadaduane)
http://blog.inquirylabs.com/
Duane Johnson (Guest)
on 2006-01-11 21:20
(Received via mailing list)
On Jan 11, 2006, at 11:51 AM, Neeraj Kumar 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 Johnson
(canadaduane)
http://blog.inquirylabs.com/
This topic is locked and can not be replied to.