Rails ActiveAdmin - 更改更新后的redirect_to

发布于 2024-12-29 19:24:01 字数 351 浏览 2 评论 0原文

我有一个属于汽车页面的功能页面。这正是我想要的,除了一件事之外。

创建、更新或销毁后,我希望页面重定向到 admin_car_path(car) 而不是默认的 admin_car_feature_path(car,feature) 用于创建和更新以及 <代码>admin_car_features_path(汽车)。

我没有成功地寻找那个。

ActiveAdmin.register Car do
end

ActiveAdmin.register Feature do
  belongs_to :car
end

TIA

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 page to be redirected to the admin_car_path(car) instead of the defaults admin_car_feature_path(car,feature) for create and update and admin_car_features_path(car).

I unsuccessfully searched for that.

ActiveAdmin.register Car do
end

ActiveAdmin.register Feature do
  belongs_to :car
end

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柏拉图鍀咏恒 2025-01-05 19:24:01

正确的更新代码而不跳过验证

controller do
  def update
    super do |success,failure|
      success.html { redirect_to collection_path }
    end
  end
end

right code for updating without skipping validation

controller do
  def update
    super do |success,failure|
      success.html { redirect_to collection_path }
    end
  end
end
つ低調成傷 2025-01-05 19:24:01

以下是您的案例的更新操作代码。此代码转到 features.rb - 管理文件:

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

这会重定向到汽车索引页面。所以你有这个想法。创建和销毁操作也是如此。

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.

鸠魁 2025-01-05 19:24:01

目前接受的答案会导致忽略验证错误。

这对我来说适用于最新版本的 ActiveAdmin 和 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  

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  
泅渡 2025-01-05 19:24:01

这是一个也适用于 create_another 的解决方案,使用 parentchild 作为模型名称。

此解决方案假设您将子项显示为父项的一部分(例如通过table_for),因此您不需要子项的index 方法。

在资源覆盖控制器的 smart_resource_urlindex 方法中:

  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

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
九歌凝 2025-01-05 19:24:01

当前的答案是跳过验证。其他一些答案有效,但部分正确(不正确使用 super 或手动验证资源)。

创建和更新后使用 AA 进行重定向的最新“正确”方式:

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

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
马蹄踏│碎落叶 2025-01-05 19:24:01

马塞洛,我不确定我是否理解您的问题,但不会将其放入控制器中的 updatecreatedestroy 操作中做这件事吗?

 format.html { redirect_to redirect_address }

并根据您的需要设置 redirect_address

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文