【问题标题】:Edit form checkbox, radio and trix editor not getting pre-populated in ActiveAdmin编辑表单复选框、收音机和 trix 编辑器未在 ActiveAdmin 中预先填充
【发布时间】:2017-02-10 14:14:03
【问题描述】:

我有一个包含复选框、矩阵编辑器、单选按钮和文本字段的表单。当我在管理界面中编辑记录时,表单会显示所有文本字段。我无法看到 trix 编辑器、复选框和单选按钮。

我知道 as: :radioas: :check_boxesas: :trix_editor 应该可以工作,但它们没有预先填充已保存的值

这是我的 jobposting.rb 文件。

任何帮助将不胜感激。

form do |f|
    f.inputs "New Job Posting" do
      f.input :title
      f.input :description, as: :trix_editor
      f.input :how_to_apply, as: :trix_editor
      f.input :job_type, as: :radio
      f.input :city
      f.input :user_id
      f.input :categories, as: :check_boxes

      f.input :publish


    end
    f.actions
  end

表格看起来像这样

它没有显示应该有 trix 编辑器的字段(描述,如何应用)。对于 radio(Job Type) 和复选框(Categories) 字段,它显示简单的是和否。这些字段的实际值是这些

已编辑

Started GET "/admin/job_postings/3/edit" for ::1 at 2017-02-11 12:41:22 +0530
Processing by Admin::JobPostingsController#edit as HTML
  Parameters: {"id"=>"3"}
  AdminUser Load (0.4ms)  SELECT  "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1  ORDER BY "admin_users"."id" ASC LIMIT 1  [["id", 1]]
  JobPosting Load (0.3ms)  SELECT  "job_postings".* FROM "job_postings" WHERE "job_postings"."id" = $1 LIMIT 1  [["id", 3]]
  CACHE (0.0ms)  SELECT  "job_postings".* FROM "job_postings" WHERE "job_postings"."id" = $1 LIMIT 1  [["id", "3"]]
  Rendered /Users/KrishnaTeja/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/activeadmin-2bb5021f81c2/app/views/active_admin/resource/edit.html.arb (270.1ms)
Completed 500 Internal Server Error in 367ms (ActiveRecord: 7.4ms)

ActionView::Template::Error (undefined method `categories' for #<Class:0x007ffdd084e420>):
    1: insert_tag renderer_for(:edit)
  app/admin/job_postings.rb:26:in `block (3 levels) in <top (required)>'
  app/admin/job_postings.rb:18:in `block (2 levels) in <top (required)>'


  Rendered /Users/KrishnaTeja/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (16.6ms)
  Rendered /Users/KrishnaTeja/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (7.9ms)
  Rendered /Users/KrishnaTeja/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.8ms)
  Rendered /Users/KrishnaTeja/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.7.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (115.2ms)

这是我的views/job_postings/_form.html.erb 如果您检查类别字段,它不是来自任何表,而是有一个数组并遍历它。

<%= form_for @job_posting do |f| %>
<% if @job_posting.errors.any? %>
        <div id="errors">
            <h2><%= pluralize(@job_posting.errors.count, "error") %> prevented this post from saving:</h2>
            <ul>
                <% @job_posting.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>

<p>
    <%= f.label :title %>
    <%= f.text_field :title %>
</p>

<p>
    <%= f.label :description %>

   <%= f.trix_editor :description %>

</p>
<p>
    <%= f.label :how_to_apply %>
    <%= f.trix_editor :how_to_apply %>
</p>
<p>
    <%= f.label :categories %>

    <% [ 'Front End', 'Back End', 'Others'].each do |category| %>
      <%= label_tag "job_posting_categories_#{category}", category %>

      <%= check_box_tag "job_posting[categories][]", category, checked(category), id: "job_posting_categories_#{category}" %> 


    <% end %>

</p>
<p>
    <%= f.label :job_type %>
    <% [ 'FullTime', 'Contract', 'PartTime', 'Freelance', 'Intern'].each do |job_type| %>
         <%= f.radio_button 'job_type', job_type %>
          <%= job_type.humanize %>
    <% end %>

</p>
<p>
    <%= f.label :city %>
    <%= f.text_field :city %>
</p>
    <%= f.submit "Post Job" %>


<% end %>

【问题讨论】:

  • 能否请您在其中发布带有check_boxesradio 之类的代码,因为正如您现在发布的那样,正在渲染的内容运行良好。我们需要根据需要查看代码,以及生成的屏幕截图
  • @LBarry 我添加了更多信息。抱歉不准确。
  • @KrishnaTejaKarnam:我们更喜欢将答案放在此处的答案框中。您是否会将其复制到自我回答中,然后在添加解决方案之前将您的问题回滚到其最新状态?谢谢。
  • @halfer 我已添加。谢谢。

标签: ruby-on-rails ruby ruby-on-rails-4 activeadmin formtastic


【解决方案1】:

已解决

ActiveAdmin.register JobPosting do

# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
permit_params :title, :description,:how_to_apply, :job_type ,:city,:user_id,{ categories: []}, :publish
#
# or
#
# permit_params do
#   permitted = [:permitted, :attributes]
#   permitted << :other if params[:action] == 'create' && current_user.admin?
#   permitted
# end


form do |f|
    inputs "New Job Posting" do

      input :title
      input :description
      input :how_to_apply
      input :job_type, as: :radio, collection: [ 'FullTime', 'Contract', 'PartTime', 'Freelance', 'Intern']
      input :city
      input :user, member_label: :email
      input :categories, as: :check_boxes, collection: ['Front End','Back End','Others'].map { |e| [e, e,{checked: f.object.categories.include?(e)}] }

      input :publish


    end
    actions
  end


end

【讨论】:

    【解决方案2】:

    trix_editor 似乎与 activeadmin 不兼容,所以我不确定您是否可以使用它。对于其他人,您需要包含一个要显示的集合,否则它似乎默认为是/否框。所以是这样的:

    f.input :categories, as: :check_boxes, collection: f.object.categories
    

    这应该让活跃的管理员知道您想使用类别可用的选项来填充复选框。

    已编辑:

    试试这个:

      form do |f|
        inputs "New Job Posting" do
          input :title
          input :description
          input :how_to_apply
          input :job_type, as: :radio
          input :city
          input :user_id
          input :categories, as: :check_boxes, collection: f.object.categories
          input :publish
        end
        actions
      end
    

    甚至:

      form do |f|
        inputs "New Job Posting" do
          input :title
          input :description
          input :how_to_apply
          input :job_type, as: :radio
          input :city
          input :user_id
          input :categories, as: :check_boxes, collection: JobPosting.categories
          input :publish
        end
        actions
      end
    

    【讨论】:

    • 我已经添加了这个 f.input :categories, as: :check_boxes, collection: f.object.categories 并且我收到了这个错误 undefined method "map" for "[\"Front End\", \"Back End\"]":String 看起来与类型转换有关。我不知道它是否缺少一个愚蠢的东西。
    • 您可能需要从集合中删除f,因此请改用collection: object.categories
    • 即使那样我也遇到同样的错误undefined method "map" for "[\"Front End\", \"Back End\"]":String
    • 我编辑了我的答案,希望能解决这个问题。如果我的编辑不起作用,请发布您收到的错误的完整堆栈跟踪,这不是我以前见过的。
    • 我还添加了堆栈跟踪和一些屏幕截图。你能检查一下吗?
    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2016-06-23
    • 2021-05-20
    相关资源
    最近更新 更多