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中顯示正常,但是,任何人都想摸一下它?
錯字,我只是用它去我知道的拼寫錯了 –
什麼答案本身...我是否正確理解你的問題?或者你可以讓事情變得更清楚 - 我會盡量做得更具體 – okliv