Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor page redirect path after 'destroy', 'create', 'update' #1995

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create

if resource.save
redirect_to(
[namespace, resource],
after_resource_created_path(resource),
notice: translate_with_resource("create.success"),
)
else
Expand All @@ -60,7 +60,7 @@ def create
def update
if requested_resource.update(resource_params)
redirect_to(
[namespace, requested_resource],
after_resource_updated_path(requested_resource),
notice: translate_with_resource("update.success"),
)
else
Expand All @@ -76,11 +76,23 @@ def destroy
else
flash[:error] = requested_resource.errors.full_messages.join("<br/>")
end
redirect_to action: :index
redirect_to after_resource_destroyed_path(requested_resource)
end

private

def after_resource_destroyed_path(_requested_resource)
{ action: :index }
end

def after_resource_created_path(requested_resource)
[namespace, requested_resource]
end

def after_resource_updated_path(requested_resource)
[namespace, requested_resource]
end

helper_method :nav_link_state
def nav_link_state(resource)
resource_name.to_s.pluralize == resource.to_s ? :active : :inactive
Expand Down
20 changes: 19 additions & 1 deletion docs/customizing_controller_actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@ end
def default_sorting_direction
:desc
end
```
```

## Customizing Redirects after actions

To set custom redirects after the actions `create`, `update` and `destroy` you can override `after_resource_created_path`, `after_resource_updated_path` or `after_resource_destroyed_path` like this:

```ruby
def after_resource_destroyed_path(_requested_resource)
{ action: :index, controller: :some_other_resource }
end

def after_resource_created_path(requested_resource)
[namespace, requested_resource.some_other_resource]
end

def after_resource_updated_path(requested_resource)
[namespace, requested_resource.some_other_resource]
end
```
41 changes: 41 additions & 0 deletions spec/controllers/admin/application_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "rails_helper"

RSpec.describe Admin::OrdersController, type: :controller do
controller(Admin::OrdersController) do
def after_resource_destroyed_path(_requested_resource)
{ action: :index, controller: :customers }
end

def after_resource_created_path(requested_resource)
[namespace, requested_resource.customer]
end

def after_resource_updated_path(requested_resource)
[namespace, requested_resource.customer]
end
end

it "redirect to custom route after destroy" do
order = create(:order)

delete :destroy, id: order.to_param
expect(response).to redirect_to(admin_customers_path)
end

it "redirect to custom route after create" do
customer = create(:customer)
order_attributes = build(:order, customer: customer).attributes
params = order_attributes.except("id", "created_at", "updated_at", "shipped_at")

post :create, order: params
expect(response).to redirect_to(admin_customer_path(customer))
end

it "redirect to custom route after update" do
order = create(:order)
order_params = { address_line_one: order.address_line_one }

put :update, id: order.to_param, order: order_params
expect(response).to redirect_to(admin_customer_path(order.customer))
end
end