【问题标题】:Render custom edit partial in a 'Custom Page' in ActiveAdmin在 ActiveAdmin 的“自定义页面”中呈现自定义编辑部分
【发布时间】:2014-10-07 20:48:53
【问题描述】:

我正在为一个项目使用 ActiveAdmin。我有并且我有一个自定义表单的部分。我希望能够在用户单击按钮时呈现此自定义表单,所以这就是我所做的:

 ActiveAdmin.register_page "Messages" do

    menu priority: 5
    welcome_message = Template.first

    page_action :update, method: :put do
      #update code
    end

    page_action :edit, :method => :get do
      render partial:'custom_form', locals: { settings: welcome_message }
    end

    action_item do
      link_to "Edit", admin_welcome_messages_edit_path, :method => :get
    end

    content do
      render text: "HI"
    end
  end

这可行,但问题是我的表单在没有 ActiveAdmin 的布局和样式的情况下呈现,它只是将我的custom_form 显示为一个干净的 html。

如果我在content do ... end 中渲染我的custom_form,它确实可以工作,但我需要它来展示一些不同的东西。

有什么帮助吗??我不知道还有什么可以尝试的,我已经到了google的前3页没有成功!!

【问题讨论】:

    标签: ruby-on-rails activeadmin


    【解决方案1】:

    有点老问题,但我遇到了同样的问题,这为我解决了。

    render partial: "name_of_your_partial", layout: "active_admin" 
    

    也不适合我,所以我只是将我的部分更改为模板。

    render: "name_of_your_template", layout: "active_admin"
    

    现在正在显示 activeadmin 布局。

    【讨论】:

    • 我在尝试render: "name_of_your_template", layout: "active_admin" 时遇到语法错误,我必须在渲染后删除冒号,如下所示:render "name_of_your_template", layout: "active_admin"
    【解决方案2】:

    我没有找到答案,但我找到了一个看起来不错的解决方法。

    action_item我有一个指向admin_welcome_messages_path 的链接,这是主视图,如果我正在编辑,请在此处添加一个参数以显示表单而不是正文。

    希望它对某人有所帮助!

      ActiveAdmin.register_page "Messages" do
    
        menu priority: 5
        welcome_message = Template.first
    
        page_action :update, method: :put do
          #update code
        end
    
        action_item do
          link_to "Edit", admin_welcome_messages_path(edit: true), :method => :get
        end
    
        content do
          if params["edit"] == "true"
            render partial:'form', locals: { settings: welcome_message }  
          else
            render partial:'body'
          end
        end
      end
    

    【讨论】:

      【解决方案3】:

      向渲染调用添加布局选项。

      render partial:'custom_form', layout: 'active_admin', locals: { settings: welcome_message }
      

      【讨论】:

      • 这不起作用,它说找不到布局。
      【解决方案4】:

      确定要使用的布局。

      1. 如果我们要呈现标准的 Active Admin 操作,我们需要 layout(false) 因为这些动作是基本页面的子类(实现 所有必需的布局代码)
      2. 如果我们要呈现自定义操作,我们将使用 active_admin 布局,因此 用户可以在 Active Admin 中呈现任何模板。

      https://github.com/activeadmin/activeadmin/commit/ce0927661c5991cc857306363c402ef9e283cc42

      # this would automatically render the custom_edit.html.erb view
      page_action :custom_edit do
      end
      
      action_item do
        link_to "Custom Edit", admin_welcome_messages_custom_edit_path
      end
      

      【讨论】:

        【解决方案5】:

        我意识到这是一个老问题,但以下问题适用于我的 Ruby on Rails 应用程序:

        在您的 your_custom_page.rb 文件中执行以下操作

        content do
           render 'partial_name'
        end
        

        并将_partial_name.erb 放入view/admin/your_custom_page/

        您的部分现在应该在该活动管理页面上呈现。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-13
          相关资源
          最近更新 更多