Neeraj Kumar
2006-Jan-11 16:56 UTC
[Rails] 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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060111/89e4939c/attachment-0001.html
Duane Johnson
2006-Jan-11 18:13 UTC
[Rails] How to display the error messages from the controller
On Jan 11, 2006, at 9:56 AM, Neeraj Kumar wrote:> 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. >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
2006-Jan-11 18:44 UTC
[Rails] How to display the error messages from the controller
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 Johnson <duane.johnson@gmail.com> wrote:> > > On Jan 11, 2006, at 9:56 AM, Neeraj Kumar wrote: > > > 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. > > > > 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/ > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060111/44f5cbd4/attachment.html
Ian Harding
2006-Jan-11 18:45 UTC
[Rails] How to display the error messages from the controller
On 1/11/06, Duane Johnson <duane.johnson@gmail.com> wrote:> > On Jan 11, 2006, at 9:56 AM, Neeraj Kumar wrote: > > > 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. > > > > 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 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
2006-Jan-11 18:51 UTC
[Rails] How to display the error messages from the controller
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 1/11/06, Ian Harding <harding.ian@gmail.com> wrote:> > On 1/11/06, Duane Johnson <duane.johnson@gmail.com> wrote: > > > > On Jan 11, 2006, at 9:56 AM, Neeraj Kumar wrote: > > > > > 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. > > > > > > > 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 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060111/d1e09944/attachment.html
Duane Johnson
2006-Jan-11 20:12 UTC
[Rails] How to display the error messages from the controller
On Jan 11, 2006, at 11:45 AM, Ian Harding wrote:>> >> 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 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
2006-Jan-11 20:18 UTC
[Rails] How to display the error messages from the controller
On Jan 11, 2006, at 11:51 AM, Neeraj Kumar wrote:> 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. > > -=- NeerajYou 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/