I am trying so learn how to do custom validations. The validation works as expected, but I can''t get "error_messages_for" to display. How is the name of the object supposed to be formated? Where is this object created, the model or controller? What am I doing wrong here? #### Model ######################## class MyFolder < ActiveRecord::Base def validate # validation test if name == ''test'' errors.add(:name, "Folder cannot be named test") end end end #### Controller ######################## class FolderController < ApplicationController def list @my_folder = MyFolder.new end def create_folder @my_folder = MyFolder.new(params[:folder]) if @my_folder.save flash[:notice] = "Folder created sucessfully" else flash[:notice] = "Error creating folder" end redirect_to :action => "list", :id => params[:id] end end #### View ######################## <%= error_messages_for(:my_folder) %> <%= start_form_tag :action => ''create_folder'' %> <%= hidden_field "folder", "parent_id", :value => @current_folder.id %> <%= text_field "folder", "name", "size" => 20 %> <%= submit_tag "New Folder" %> <%= end_form_tag %> -- Posted via http://www.ruby-forum.com/.
Does anyone know how to do this? I still can''t figure this out. I am trying so learn how to do custom validations. The validation works as expected, but I can''t get "error_messages_for" to display. How is the name of the object supposed to be formated? Where is this object created, the model or controller? What am I doing wrong here? #### Model ######################## class MyFolder < ActiveRecord::Base def validate # validation test if name == ''test'' errors.add(:name, "Folder cannot be named test") end end end #### Controller ######################## class FolderController < ApplicationController def list @my_folder = MyFolder.new end def create_folder @my_folder = MyFolder.new(params[:folder]) if @my_folder.save flash[:notice] = "Folder created sucessfully" else flash[:notice] = "Error creating folder" end redirect_to :action => "list", :id => params[:id] end end #### View ######################## <%= error_messages_for(:my_folder) %> <%= start_form_tag :action => ''create_folder'' %> <%= hidden_field "folder", "parent_id", :value => @current_folder.id %> <%= text_field "folder", "name", "size" => 20 %> <%= submit_tag "New Folder" %> <%= end_form_tag %> -- Posted via http://www.ruby-forum.com/.
>From the documentation examples :errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/ have you tried to use "name" instead of :name ? class MyFolder < ActiveRecord::Base def validate # validation test if name == ''test'' errors.add("name", "Folder cannot be named test") # name is a string not a symbol ... end end end jean On 2/15/06, Scott NJ <scottnj@gmail.com> wrote:> Does anyone know how to do this? I still can''t figure this out. > > I am trying so learn how to do custom validations. > The validation works as expected, but I can''t get "error_messages_for" > to display. > How is the name of the object supposed to be formated? > Where is this object created, the model or controller? > What am I doing wrong here? > > #### Model ######################## > class MyFolder < ActiveRecord::Base > def validate > # validation test > if name == ''test'' > errors.add(:name, "Folder cannot be named test") > end > end > end > > #### Controller ######################## > class FolderController < ApplicationController > def list > @my_folder = MyFolder.new > end > def create_folder > @my_folder = MyFolder.new(params[:folder]) > if @my_folder.save > flash[:notice] = "Folder created sucessfully" > else > flash[:notice] = "Error creating folder" > end > redirect_to :action => "list", :id => params[:id] > end > end > > #### View ######################## > <%= error_messages_for(:my_folder) %> > <%= start_form_tag :action => ''create_folder'' %> > <%= hidden_field "folder", "parent_id", :value => @current_folder.id %> > <%= text_field "folder", "name", "size" => 20 %> > <%= submit_tag "New Folder" %> > <%= end_form_tag %> > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
My problem was I was "redirecting_to", when I changed to "render" everything worked like I expected. Jean Helou wrote:>>From the documentation examples : > errors.add("phone_number", "has invalid format") unless phone_number =~ > /[0-9]*/ > > have you tried to use "name" instead of :name ? > > class MyFolder < ActiveRecord::Base > def validate > # validation test > if name == ''test'' > errors.add("name", "Folder cannot be named test") # name is a > string not a symbol ... > end > end > end > > jean-- Posted via http://www.ruby-forum.com/.
Thank you for posting your solution. I had the same exact problem, using redirect_to with error_messages_for Once I switched to render it fixed my problem as well. Thanks! Scott NJ wrote:> My problem was I was "redirecting_to", when I changed to "render" > everything worked like I expected. > > > Jean Helou wrote: >>>From the documentation examples : >> errors.add("phone_number", "has invalid format") unless phone_number =~ >> /[0-9]*/ >> >> have you tried to use "name" instead of :name ? >> >> class MyFolder < ActiveRecord::Base >> def validate >> # validation test >> if name == ''test'' >> errors.add("name", "Folder cannot be named test") # name is a >> string not a symbol ... >> end >> end >> end >> >> jean-- Posted via http://www.ruby-forum.com/.
The problem with using render instead of redirect_to is that it breaks redirect-on-post. Try running that page and causing the error. You get the error message, right? But now try hitting the reload button. You get the "the page ... contained post data" error from your browser, right? That''s because calling render in the case of error just goes straight to the view rather than redirecting the browser to the view. So, the post url (the "action" attribute" of your form) is left in the browser''s address bar. Now, there are lots of apps out there that do this, so it''s not the end of the world. But, it''s not very user-friendly. And, you can still redirect and get the errors by saving the model object or the errors object in the flash. That''s what I did and it works fine. Oh, and I''m also writing this based off of what I saw below... missed the begining of the thread. So, if I''m completely misunderstanding everything here, just pat me on the head, give me a cookie and tell me to run along. b Guest wrote:> Thank you for posting your solution. I had the same exact problem, > using redirect_to with error_messages_for > > Once I switched to render it fixed my problem as well. > > Thanks! > > > > Scott NJ wrote: > >>My problem was I was "redirecting_to", when I changed to "render" >>everything worked like I expected. >> >> >>Jean Helou wrote: >> >>>>From the documentation examples : >>>errors.add("phone_number", "has invalid format") unless phone_number =~ >>>/[0-9]*/ >>> >>>have you tried to use "name" instead of :name ? >>> >>>class MyFolder < ActiveRecord::Base >>> def validate >>> # validation test >>> if name == ''test'' >>> errors.add("name", "Folder cannot be named test") # name is a >>>string not a symbol ... >>> end >>> end >>>end >>> >>>jean > >
Scott NJ wrote:> I am trying so learn how to do custom validations. > The validation works as expected, but I can''t get "error_messages_for" > to display. > How is the name of the object supposed to be formated? > Where is this object created, the model or controller? > What am I doing wrong here? > > #### Model ######################## > class MyFolder < ActiveRecord::Base > def validate > # validation test > if name == ''test'' > errors.add(:name, "Folder cannot be named test") > end > end > end > > #### Controller ######################## > class FolderController < ApplicationController > def list > @my_folder = MyFolder.new > end > def create_folder > @my_folder = MyFolder.new(params[:folder]) > if @my_folder.save > flash[:notice] = "Folder created sucessfully" > else > flash[:notice] = "Error creating folder" > end > redirect_to :action => "list", :id => params[:id] > end > end > > #### View ######################## > <%= error_messages_for(:my_folder) %> > <%= start_form_tag :action => ''create_folder'' %> > <%= hidden_field "folder", "parent_id", :value => @current_folder.id %> > <%= text_field "folder", "name", "size" => 20 %> > <%= submit_tag "New Folder" %> > <%= end_form_tag %>I had this same problem too when using a ''redirect_to''. Glad it worked out for you!. One other thing you may try is to move the ''redirect_to'' up into your IF statement. That way, when the save works, the user is sent to ''list''. On the flip-side, if the save fails, the form (create_folder) is shown again and the user gets your error messages. -- Posted via http://www.ruby-forum.com/.