0

我有一个 Rails 应用程序,我使用 Active Admin 作为后端。Active Admin 使用 Formtastic,我无法将 _form.html.erb 中的一个输入字段转换为帖子。

这是表格...

<div class="container">
<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :author %>
    <%= f.collection_select :blog_id, Blog.all, :id, :name, prompt: "Select a blog" %>        
  </div>
  <div class="field">
    <p>
      Categories:<br>
      <%= hidden_field_tag "post[category_ids][]", nil %>
      <% Category.all.each do |category| %>
        <%= check_box_tag "post[category_ids][]", category.id, @post.category_ids.include?(category.id), id: dom_id(category) %> 
        <%= label_tag dom_id(category), category.name %>
      <% end %>    
    </p>
  </div>
  <div class="field">
    <%= f.hidden_field :status, value: "Draft" %> 
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body, class: "redactor", id: "redactor" %>
  </div>
  <div class="field">
    <%= f.label :published_on %><br />
    <%= f.date_select :published_on %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
</div>

这是我无法转换为特定格式的代码的部分。它来自 Ryan 在HABTM Checkboxes上的截屏视频。

<div class="field">
    <p>
      Categories:<br>
      <%= hidden_field_tag "post[category_ids][]", nil %>
      <% Category.all.each do |category| %>
        <%= check_box_tag "post[category_ids][]", category.id, @post.category_ids.include?(category.id), id: dom_id(category) %> 
        <%= label_tag dom_id(category), category.name %>
      <% end %>    
    </p>
  </div>

这是 Active Admin 的 admin/post.rb 文件中的表单代码。如果我使用它,我将继续收到类别的无方法错误。我知道它目前列出的方式是不正确的。

form do |f|
    f.inputs "Details" do
        f.input :blog, hint: "Select a blog"
        f.input :title, hint: "Enter blog post title"
        f.input :published_on, as: :date, include_blank: false, hint: "Select a date", :prompt => {:day => "Day", :month => "Month", :year => "Year"}, :start_year => Time.now.year
        f.input :status, as: :select, collection: ["Draft", "Published"], include_blank: false, hint: 'Select "Draft" to save and post later, and "Publish" to post now'

        f.input :categories, as: :check_boxes

        f.input :body, input_html: { class: "redactor", id: "redactor" }
    end
    f.buttons
end

如果有人有任何意见,我将不胜感激。我查看了 formtastic 文档并观看了 Ryan 的截屏视频,但仍然遇到转换问题。

谢谢!

编辑:能够解决下面的解决方案

在对 Formtastic 和 Active Admin 更加熟悉之后,我能够完成这项工作。以下是 Active Admin 中的表单、显示和索引视图中的相关代码。

ActiveAdmin.register Post do
index do
    column "Post Title", :title
    column :blog
    column :published_on
    column :status
    column "Category", :categories do |post|
        post.categories.collect { |cat|  cat.name }.join(", ")
    end
    default_actions
end 

show do
    h5 "Created by #{post.blog.name} on #{post.created_at.strftime('%B %-d, %Y')}"
    h5 "Categories: #{post.categories.collect { |cat|  cat.name }.join(", ")}"
    h5 "Current Status: #{post.status}"
    h5 "Published On Date: #{post.published_on.strftime('%B %-d, %Y')} - (will only be posted if marked with Publish as status)"
    div do
        simple_format post.body
    end
end

form do |f|
    f.inputs "Details" do
        f.input :blog, hint: "Select a blog"
        f.input :title, hint: "Enter blog post title"
        f.input :published_on, as: :date, include_blank: false, hint: "Select a date", :prompt => {:day => "Day", :month => "Month", :year => "Year"}, :start_year => Time.now.year
        f.input :status, as: :select, collection: ["Draft", "Published"], include_blank: false, hint: 'Select "Draft" to save and post later, and "Publish" to post now'
        f.input :categories, as: :check_boxes, collection: Category.all
        f.input :body, input_html: { class: "redactor", id: "redactor" }
    end
    f.buttons
end  
end
4

1 回答 1

0

我能够构建 Formtastic 版本,如下所示。对 Formtastic 有了更多的了解,然后对 Active Admin 进行了微调。

以下是 Active Admin 中的表单、显示和索引视图中的相关代码。

ActiveAdmin.register Post do
index do
    column "Post Title", :title
    column :blog
    column :published_on
    column :status
    column "Category", :categories do |post|
        post.categories.collect { |cat|  cat.name }.join(", ")
    end
    default_actions
end 

show do
    h5 "Created by #{post.blog.name} on #{post.created_at.strftime('%B %-d, %Y')}"
    h5 "Categories: #{post.categories.collect { |cat|  cat.name }.join(", ")}"
    h5 "Current Status: #{post.status}"
    h5 "Published On Date: #{post.published_on.strftime('%B %-d, %Y')} - (will only be posted if marked with Publish as status)"
    div do
        simple_format post.body
    end
end

form do |f|
    f.inputs "Details" do
        f.input :blog, hint: "Select a blog"
        f.input :title, hint: "Enter blog post title"
        f.input :published_on, as: :date, include_blank: false, hint: "Select a date", :prompt => {:day => "Day", :month => "Month", :year => "Year"}, :start_year => Time.now.year
        f.input :status, as: :select, collection: ["Draft", "Published"], include_blank: false, hint: 'Select "Draft" to save and post later, and "Publish" to post now'
        f.input :categories, as: :check_boxes, collection: Category.all
        f.input :body, input_html: { class: "redactor", id: "redactor" }
    end
    f.buttons
end  
end
于 2012-11-30T00:46:33.697 回答