【问题标题】:Rails ActiveAdmin - Edit the new resource-viewRails ActiveAdmin - 编辑新的资源视图
【发布时间】:2018-08-13 18:07:18
【问题描述】:

我将 ActiveAdmin 添加到我的应用程序并成功更改了资源的索引方法。现在,当我单击“新资源”时,它会将我带到新方法,但是,缺少一个按钮(回形针)以允许用户上传图像附件。

我找不到编辑视图的方法,也找不到完全重写新方法的方法。

如果你需要我的任何代码,我可以在这里粘贴所有内容。

谢谢! // 查看这篇文章的最底部的解决方案!

//这是我尝试过的方法,但它不起作用。我为索引方法应用到“app/admin/entry.rb”的更改有效,但“新”方法根本不起作用。

app/admin/entry.rb:

ActiveAdmin.register Entry do

  index do
    column :id
    column :description
    column :created_at
    column :image_content_type
    column do |entry|
      links = link_to "Edit", edit_admin_entry_path(entry)
      links += " "
      links += link_to "Delete", admin_entry_path(entry), :method => :delete, data: { confirm: "Are you sure?" }
      links
    end
  end

  def new
    form_for @entry, :html => {:multipart => true} do |f|
      f.label :description
      f.text_area :description
      f.file_field :image
    end
    f.submit 'Save'
  end

end

在添加 ActiveAdmin 之前,我只是为 Entry 添加了一个脚手架,并像这样使用它: entry_controller.rb:

  def new
    @entry = Entry.new
  end

查看(new.html.slim):

h1 New entry

== render 'form'

= link_to 'Back', entries_path

呈现的表单(_form.html.slim):

= form_for @entry, :html => {:multipart => true} do |f|
  - if @entry.errors.any?
    #error_explanation
      h2 = "#{pluralize(@entry.errors.count, "error")} prohibited this entry from being saved:"
      ul
        - @entry.errors.full_messages.each do |message|
          li = message

  .field
    = f.label :description
    = f.text_area :description
    = f.file_field :image
  .actions = f.submit 'Save'

现在,虽然这在前往 localhost:3000/entries/new 时仍然有效,但它只是显示 localhost:3000/admin/entries/new 的默认视图

如果您有任何帮助,我们将不胜感激! 有什么方法可以查看 ActiveAdmin 已经以某种方式使用的现有代码?我可以通过简单地添加我需要的一个字段来将其更改为我的需要。

// 解决方案:

app/admin/resource.rb

ActiveAdmin.register Entry do
  permit_params :image, :description

  index do
    column :id
    column :description
    column :created_at
    column :image_file_name
    column :image_content_type
    column do |entry|
      links = link_to "Edit", edit_admin_entry_path(entry)
      links += " "
      links += link_to "Delete", admin_entry_path(entry), :method => :delete, data: { confirm: "Are you sure?" }
      links
    end
  end

  form do |f|
    f.inputs "New Entry" do
      f.input :description
      f.input :image
    end
    f.actions
  end

    

end

【问题讨论】:

    标签: ruby-on-rails activeadmin


    【解决方案1】:

    您可以自定义控制器操作和新资源视图。

    在控制器中编辑新动作:

    #app/admin/your_resource.rb
    
    controller do
      def new
        @resource = Resource.new
        .... # Your custom logic goes here
      end
    end
    

    编辑新资源视图并添加带有回形针的图像。

    #app/admin/your_resource.rb
    
    form html: { multipart: true } do |f|
      f.inputs "Resource Details" do
        f.input :title
        .... # Your input fields
    
        # This adds the image field. Be careful though 
        # the field name needs to be the same in your model
    
        f.input :image, as: :file, required: false
      end
    
      f.actions
    end
    

    【讨论】:

    • 非常感谢,我过几分钟试试看!
    • 欢迎您!您也可以查看 active_admin 的 wiki。 activeadmin.info/docs/8-custom-actions.html
    • 嘿,我尝试应用更改,但它似乎不起作用。我用所有必要的代码和进一步的解释编辑了我的第一篇文章。如果你能再看看它,那就太棒了。
    • 好吧,没关系。我将从现在开始完全按照人们告诉我的方式去做。我用实际的解决方案再次更新了我的第一篇文章。我唯一的问题是为什么我必须指定 INDEX 方法,但是,当我想更改 VIEW 时我只需要“表单”?我没有看到记录在哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    相关资源
    最近更新 更多