2

Problem is simple (solution is not): I would like to redirect to edit path after X model is created (I'll apply this to multiple models).

Are there any way to do it? I'm digging in source code, trying to find a reference to created instance, but can only find instance variables named after model (like @user, which is harder to make generic).

So far, I found this answer which partially solves my problem: https://stackoverflow.com/a/22486025/312907

I'm still missing a reference to created model object.

Community
  • 1
  • 1
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147

3 Answers3

2

The created model object is accessible in the controller method as variable resource.

zwippie
  • 15,050
  • 3
  • 39
  • 54
1

You can redirect to edit action after creating the object from with in the controller itself for example

  def create
    @x = X.new(x_params)
    if @x.save
      redirect_to edit_x_path(@x)
    else
      render 'new'
    end
  end
bhanu
  • 2,260
  • 3
  • 24
  • 35
  • Sorry, this is related specifically to activeadmin, not to rails only, so I wanted to use the resource generated by activeadmin, I don't have control over how's generated. – Francesco Belladonna Oct 29 '14 at 11:02
1

In ActiveAdmin, just modify smart_resource_url, so that you don't have to mess with the create or update code:

controller do 
  def smart_resource_url
    if create_another?
      new_resource_url(create_another: params[:create_another])
    else
      edit_resource_url
    end
  end
end

If you want it to go back to the list view, put "collection_url" after the else.

If you want it to go back to the edit page, put "edit_resource_url" after the else.

Note that this redirect occurs BOTH after creating a new resource and after editing one.

Sprachprofi
  • 1,229
  • 12
  • 24