Francesco Belladonna
Francesco Belladonna

Reputation: 11689

After create, redirect to edit_path instead of show path

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.

Upvotes: 2

Views: 2291

Answers (3)

Sprachprofi
Sprachprofi

Reputation: 1259

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.

Upvotes: 1

bhanu
bhanu

Reputation: 2460

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

Upvotes: 1

zwippie
zwippie

Reputation: 15515

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

Upvotes: 2

Related Questions