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

Redirect after create. Remove edit and update actions #56

Closed
slenderock opened this issue Oct 23, 2017 · 7 comments
Closed

Redirect after create. Remove edit and update actions #56

slenderock opened this issue Oct 23, 2017 · 7 comments

Comments

@slenderock
Copy link

Hi there
First of all thanks a lot for your work. It works good and looks beauty.

Can I somehow change redirect path after create? (want change it to index action)
Also I want remove ability to edit and update record?

I found readonly: true mode, but I want to keep [:new, :create, :delete] actions

@slenderock slenderock changed the title Redirect after create. Remove edit and update action Redirect after create. Remove edit and update actions Oct 23, 2017
@spohlenz
Copy link
Member

Hi @slenderock. Sorry for the delay in looking into this. I have now implemented both of these features in master.

To change the redirect path, you can use the return_to method within a resource definition. It accepts an optional :on option to specify the action, along with a block which is evaluated within the context of the admin class. e.g.

Trestle.resource(:articles) do
  # Update return location for a single action
  return_to on: :create do |instance|
    path(:edit, id: to_param(instance))
  end

  # Update return locations for create, update and destroy
  return_to do
    path(:index)
  end
end

I have also implemented the remove_action method to properly remove unwanted resource actions. Relevant buttons (e.g. save, delete, etc) will also be removed when removing an action.

Trestle.resource(:articles) do
  remove_action :edit, :update
end

@hdbreaker
Copy link

Hi I'm not been able to reproduce the redirect behaviour:

The overwriting function is triggered:

return_to do
    path(:index)
end

But path() function is undefined.

@maikokuppe
Copy link

Hi I'm not been able to reproduce the redirect behaviour:

The overwriting function is triggered:

return_to do
    path(:index)
end

But path() function is undefined.

Same for me.

It worked for me like this:

return_to on: :destroy do |instance|
  edit_orders_admin_path(id: instance.order_id)
end

This returns to the edit view of the Order which the instance belongs to. There are also methods for other relations and methods. Just run methods in the debugger in that block.

@spohlenz
Copy link
Member

The return_to blocks are now evaluated within the context of the controller rather than the admin.

Here is an up-to-date version of my previous example:

Trestle.resource(:articles) do
  # Update return location for a single action
  return_to on: :create do |instance|
    admin.instance_path(instance, action: :edit)
  end

  # Update return locations for create, update and destroy
  return_to do
    admin.path(:index)
  end
end

@LimeBlast
Copy link

I'm trying to use this to redirect after a delete, but I'm running into some trouble.

For context, we're displaying a list of Block records within the Page (block belongs_to page) admin interface, and are using the standard actions method to display the delete button for each of these.

Once a Block has been deleted, we want to redirect it back to the Page admin, so I've added the following to the block_admin.rb file:

  return_to on: :destroy do |instance|
    admin.instance_path(instance.page, action: :edit)
  end

But it isn't working, resulting in the error Couldn't find Cms::Block with 'id'=72 because it's redirected to /trestle/blocks/72/edit (rather than /trestle/pages/72/edit)

I'm sure this is just a case of passing the correct parameter to the instance_path method to have it redirect to the pages admin, but I can't work out what this should be.

Thank you.

@spohlenz
Copy link
Member

@LimeBlast Since you are within the blocks admin, the admin in your snippet refers to the BlockAdmin class (and its routes), rather than the desired PageAdmin. Try replacing admin with PageAdmin to reference it directly.

@LimeBlast
Copy link

LimeBlast commented Dec 2, 2019

Thank you, @spohlenz - I think you meant PagesAdmin, rather than PageAdmin, but I've updated it to the following, which seems to work:

  return_to on: :destroy do |instance|
    PagesAdmin.instance_path(instance.page, action: :edit)
  end

UPDATE: or maybe you did mean PageAdmin, and I've named it wrong in my codebase - either way, the above works, Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants