2017-02-10 63 views
1

我有一個窗體,有複選框,trix編輯器,單選按鈕和文本字段。當我在管理界面中編輯記錄時,窗體將顯示所有文本字段。我無法看到trix編輯器,複選框和單選按鈕。編輯表單複選框,收音機和trix編輯器沒有得到預填充ActiveAdmin

我知道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 

和形式出現這樣

enter image description here

它沒有顯示的字段(說明,如何申請),它應該有TRIX編輯器。對於電臺(作業類型)和複選框(分類)字段顯示其簡單的是和no..where作爲這些字段的實際值是這些enter image description here

EDITED

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) 

enter image description hereenter image description here

這是我views/job_postings/_form.html.erb 如果選中categorie它不是來自任何有數組並遍歷它的表。

<%= 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 %> 
+0

能否請您發佈的代碼與'check_bo xes'和'radio'等等,因爲就像你現在發佈的那樣,呈現的東西是完美的。我們需要根據需要查看代碼,然後生成屏幕截圖 – LBarry

+0

@LBarry我添加了更多信息。對不起,不準確。 –

+0

@KrishnaTejaKarnam:我們更喜歡在這裏回答問題的答案。您是否會將其複製到自我回答中,然後在添加解決方案之前將問題回滾到最新狀態?謝謝。 – halfer

回答

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 
0

這樣看來,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 
+0

我已經添加了這個'f.input:categories,如::check_boxes,collection:f.object.categories',我得到這個錯誤'undefined method map'for「[\」Front End \「,\」Back End \「]」:String'看起來像是與類型轉換相關的東西。我不知道它是否缺少一件愚蠢的東西。 –

+0

您可能需要從集合中刪除'f',所以改爲使用'collection:object.categories' – LBarry

+0

,即使這樣我也會得到同樣的錯誤'undefined method map'for [\「Front End \」,\ 「後端\」]「:字符串' –