Tuesday, May 15, 2012

[Rails] Re: How to handle ActiveRecord::RecordInvalid

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.

On Tuesday, May 15, 2012 9:17:00 AM UTC-7, Zeck wrote:
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.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/p8zG1aRSQh0J.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate