Hi Walter,
The validate* methods works fine on the console. The function
object.valid? returns false if any of the validate method fails. For
that case object.valid? returns false even in the controller function,
but when i redirect to the same url, it doesn't show any errors from the
code
The errors are set in the event loop of reading the form submission and showing the results, but when you redirect, you start a new loop, and so unless you disrupt the cycle you seem to have set for yourself, you're never going to see those errors, because the errors -- and I suspect the object -- does not exist in the context of the page you redirected to.
In a normal scaffold controller, you will see this pattern:
def new
@my_model = MyModel.new(params[:my_model])
end
def create
@my_model = MyModel.new(params[:my_model])
if @my_model.save
redirect_to some_route(@my_model), :notice => 'Yay!'
else
render :action => 'new'
end
end
If both sides of your test for save are redirects, then you have to set the :warn or :error flash to your errors, because they will not persist after the redirect. Especially because you're using Active Model, but particularly because you're in a #create method -- each time that runs, the model instance is created fresh from the supplied parameters, and it forgets all about the botched save.
<%= f.error_messages %>
In my view im not using any sort of this code
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
Do i need it?
It's what I use, it may be out of date, since I am using Rails 3.0 for this app that I copied it from. I believe that some of this was streamlined in 3.2. But I am also sure that you have to use /some/ sort of code to indicate where you want your errors to appear on your application layout file.
Walter