Rails ActiveAdmin - change the after update redirect_to

前端 未结 6 1164
青春惊慌失措
青春惊慌失措 2020-12-31 04:01

I have a Feature page that belongs to the Car page. That is working exactly how I want to, except for one thing.

After creating, updating or destroying, I want the

相关标签:
6条回答
  • 2020-12-31 04:15

    Here is a solution that also works with create_another, using parent and child for model names.

    This solution assumes that you show children as part of parent (e.g. via table_for) so you do not need child's index method.

    In resource override controller's smart_resource_url and index methods:

      controller do
        def smart_resource_url
          if create_another?
            new_resource_url(create_another: params[:create_another])
          else
            parent_path(params[:parent_id])
          end
        end
    
        def index
          redirect_to parent_path(params[:parent_id])
        end
      end
    
    0 讨论(0)
  • 2020-12-31 04:23

    Current answer is skipping validations. Some of the other answers are working but partially correct (incorrect use of super or manually validating resource).

    Most updated "proper" way to redirect with AA after create and udpate:

    controller do
      def create
        create! do |success,failure|
          success.html { redirect_to collection_path, notice: "#{resource.model_name.human} was successfully created." }
        end
      end
      def update
        update! do |success,failure|
          success.html { redirect_to collection_path, notice: "#{resource.model_name.human} was successfully updated." }
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-31 04:25

    Here is the code for update action for your case. This code goes to the features.rb - admin file:

    controller do
      def update
        update! do |format|
          format.html { redirect_to admin_cars_path }
        end
      end
    end
    

    This redirects to the cars index page. So you have the idea. Same for create and destroy actions.

    0 讨论(0)
  • 2020-12-31 04:26

    Marcelo, I'm not sure I understand your question, but wouldn't putting this into the update, create and destroy actions in your controller do the trick?

     format.html { redirect_to redirect_address }
    

    And make redirect_address whatever you need.

    0 讨论(0)
  • 2020-12-31 04:34

    right code for updating without skipping validation

    controller do
      def update
        super do |success,failure|
          success.html { redirect_to collection_path }
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-31 04:36

    At the current moment accepted answer leads to ignoring validation errors.

    This works for me with the latest versions of ActiveAdmin and Rails:

    controller do
    
      def update
        update! do |format|
          format.html { redirect_to collection_path } if resource.valid?
        end
      end
    
      def create
        create! do |format|
          format.html { redirect_to collection_path } if resource.valid?
        end
      end
    
    end  
    
    0 讨论(0)
提交回复
热议问题