ruby on rails - DRY in controller -


i want improve code in controller according dry.

  def create     blog.user = current_user     blog.save     respond_with blog, location: user_root_path   end    def update     blog.update(blog_params)     respond_with blog, location: user_root_path   end    def destroy     blog.destroy     respond_with blog, location: user_root_path   end 

every method has respond_with blog, location: user_root_path. how can hide it?

you can :after_action filter

after filters run after action completes. can modify response. of time if done in after filter, can done in action itself, if there logic run after running of set of actions, after filter place it.

:after_action :responding, only: [:create, :update, :destroy]  def create   blog.user = current_user   blog.save end  def update   blog.update(blog_params) end  def destroy   blog.destroy end  def :responding   respond_with blog, location: user_root_path end 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -