2013-11-20 59 views
0

我想添加名作我的帖子有has_and_belongs_to_many关系 我“敢接近,但只是一点点了。添加类别职位轨

这里是我的岗位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> 

现在在新的它只是说,产品类别在这三个名作我在轨道控制台在我的岗位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中显示正常,但是,任何人都想摸一下它?

回答

0

根据this轨投你需要在这里

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

错字,我只是用它去我知道的拼写错了 –

+0

什么答案本身...我是否正确理解你的问题?或者你可以让事情变得更清楚 - 我会尽量做得更具体 – okliv