How to handle ActiveRecord::RecordInvalid

39 views
Skip to first unread message

Zeck

unread,
May 15, 2012, 12:17:00 PM5/15/12
to rubyonra...@googlegroups.com
I'm new to Rails. I'm using Service layer to keep my controllers thin. All my service layer files are located in app/services/domainapp/services/application and app/services/infrastructure. I'm using services in active admin controller section. When validation fail it can't redirect correctly, it keeps throwing me ActiveRecord::RecordInvalid. Here is the error page: 

And here is the category service:

def self.create(params)
   
ActiveRecord::Base.transaction do
       
begin
            category
= Category.new(params)
            decrease_counters
(category.id)
            increase_counters
(category.id)
                category
.save!
       
end
   
end
end

In finally here is the my controller:

  controller do

   
def create
      category
= Service::CategoryService.create(params[:category])
     
if category
        flash
[:notice] = "Category was successfully created."
        redirect_to params
[:button] == "create_and_new" ? new_admin_category_url : admin_categories_url
     
else
        render active_admin_template
('new.html.erb')
     
end
   
end
 
end # end controller

How to solve it? Please help me.


Tyler

unread,
May 15, 2012, 6:04:20 PM5/15/12
to rubyonra...@googlegroups.com
This all seems very unnecessary.  Have you tried pushing the business logic into the Category model instead?

Best thing with Rails is to follow convention (that is kind of the point of Rails).  You can go through www.railstutorial.org and get a much better sense for how to structure your controllers and models.
Reply all
Reply to author
Forward
0 new messages