为帖子栏添加类别

时间:2013-11-20 17:12:37

标签: ruby-on-rails

我正在尝试使用has_and_belongs_to_many关系将catagories添加到我的帖子中 我非常接近但只是稍微休息一下。

这是我的帖子index.html.erb

<div class="container">
  <div class="row">
    <div class="col-lg-12">
      <h1 class="page-header">Post Index<small> by title</small></h1>
    </div>
  </div>

  <%= form_tag posts_path, :method => :get do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
    <% end %>
    <center><%= link_to 'Create New Post', new_post_path %></center>
  </p>


  <p><% @posts.sort_by(&:comments_count).reverse.each do |post| %></p>
  <p><%= image_tag avatar_url(post.user), size: "31x31" %> <%= post.user.name %></p>
  <p><strong>Title: </strong><%= post.title %></p>
  <p><strong>Summary: </strong><%= post.summary %></p>
  <p><%= post.catagories.name %>
  <p><%= link_to 'Read Full Post', post %> Total Comments for post . . . (<%= post.comments.count %>)</p>
  <p><strong>Posted ON: </strong><%= post.created_at.strftime('%b %d, %Y') %></p>
  <br>
  <p><% if current_user.admin? %><%= link_to "delete", post, method: :delete, data: { confirm: "You sure" } %>
  <% end %>
  <% if current_user.admin? %><%= link_to 'Edit', edit_post_path(post) %><% end %></p>
  <% end %>
</div>

catagory.rb文件

class Catagory < ActiveRecord::Base
  has_and_belongs_to_many :posts
end

post.rb文件

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  has_and_belongs_to_many :catagories
  mount_uploader :image, ImageUploader

  searchable do
    text :title, :boost => 5
    text :content
  end

   def comments_count
    comments.count
  end

end

发布_form.html.erb

<%= form_for @post, :html => {:multipart => true} 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.file_field :image %>
</div>
<div class="field">
  <%= f.label :summary %><br>
  <%= f.text_field :summary %>
</div>
<div class="field">
 <%= f.label :content %><br>
 <%= f.text_area :content %>
</div>
<div class="field">
  <% Catagory.all.each do |catagory| %>
  <%= check_box_tag catagory.id %>
  <%= catagory.name %><br/>
  <% end %><br>
  <div class="actions">
    <%= f.submit %>
  </div>
  <% end %>
</div>

现在在新的它只是说Catagory而不是我在我的帖子index.html.erb文件中设置的三个类别中的一个

#<ActiveRecord::Relation [#<Catagory id: 1, name: "lolcats", created_at: "2013-11-20 15:17:05", updated_at: "2013-11-20 15:17:05">, #<Catagory id: 2, name: "surfing", created_at: "2013-11-20 15:17:42", updated_at: "2013-11-20 15:17:42">, #<Catagory id: 3, name: "dance", created_at: "2013-11-20 15:18:11", updated_at: "2013-11-20 15:18:11">]>

我的帖子_form中的复选框很好,但有人觉得要刺它吗?

1 个答案:

答案 0 :(得分:0)

根据this rails cast,你需要在这里更改check_box_tag

 <% Catagory.all.each do |catagory| %>
   <%= check_box_tag catagory.id %>
   <%= catagory.name %><br/>
 <% end %><br>

<%= check_box_tag "post[catagory_ids][]", catagory.id, @post.catagories.include?(catagory) %> #why catAgory, by the way?