How can you pass an error messages coming from a model –> controller to view?
= form_tag :controller => "article", :action => "create" do
/ how to retrieve error messages here?
%p
= label_tag :article, "Article"
= text_field_tag :article
= submit_tag "Submit Article"
I have this model:
class Article < ActiveRecord::Base
attr_accessible :article
validates :article, :presence => true
end
In my controller:
def create
@article = Article.new(params[:article])
if ! @article.save
# how to set errors messages?
end
end
I’m using Rails 3.0.9
The errors messages are stored in your model. You can access through the errors methods, like you can see in http://api.rubyonrails.org/classes/ActiveModel/Errors.html.
An easy way to expose the error message is including the follow line in your view:
But, I belive you have to change your controller to be like that:
In the new action you don’t need to try save the article, because the creation action already do that job. The new action exists, (basically) to call the new view and to provide support for validations messages.