Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@armstrongnate
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armstrongnate/4e94e38c04e86a6990ad to your computer and use it in GitHub Desktop.
Save armstrongnate/4e94e38c04e86a6990ad to your computer and use it in GitHub Desktop.
example of a many to many relationship in rails
class Post < ActiveRecord::Base
has_many :post_tags
has_many :tags, through: :post_tags
end
class PostTag < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
<!-- app/views/posts/_form.html.erb -->
<%= form_for @post do |f| %>
<div class='row'>
<div class='col-md-6'>
<div class='form-group'>
<%= f.label :title, class: 'form-label' %>
<%= f.text_field :title, class: 'form-control' %>
</div>
</div>
<div class='col-md-6'>
<div class='form-group'>
<%= f.label :tags, class: 'form-label' %>
<%= f.select :tag_ids, options_from_collection_for_select(Tag.all, 'id', 'name'), {}, { multiple: true, class: 'form-control' } %>
</div>
</div>
<div class='col-md-12'>
<div class='form-group'>
<%= f.label :content, class: 'form-label' %>
<%= f.text_area :content, class: 'form-control', rows: 20 %>
</div>
</div>
<div class='col-md-12'>
<%= f.submit 'Submit', class: 'btn btn-primary' %>
</div>
</div>
<% end %>
<!-- app/views/posts/index.html.erb -->
<h1>Posts</h1>
<div class='row'>
<div class='col-md-7'>
<% @posts.each do |post| %>
<div class='post'>
<h2><%= post.title %></h2>
<hr>
<div>
<%= truncate(post.content, length: 200) %>
</div>
<% if post.tags.any? %>
<div class='tags'>
<strong>Tags:</strong>
<% post.tags.each do |tag| %>
<%= link_to tag do %>
<span class='label label-default'><%= tag.name %></span>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
<div class='col-md-5'>
<%= link_to 'New Post', new_post_path, class: 'btn btn-warning pull-right' %>
</div>
</div>
class PostsController < ApplicationController
respond_to :html
def index
@posts = Post.all
end
def new
@post = Post.new
end
def create
@post = Post.create(safe_params)
respond_with @post
end
private
def safe_params
safe_attributes = [
:title,
:content,
tag_ids: [],
]
params.require(:post).permit(*safe_attributes)
end
end
bin/rails g model Post title content:text
bin/rails g model Tag name
bin/rails g model PostTag post:belongs_to tag:belongs_to
bin/rake db:migrate
<!-- app/views/tags/show.html.erb -->
<h1><%= "#{@tag.name.pluralize} Posts" %></h1>
<ul>
<% @tag.posts.each do |post| %>
<li><%= link_to post.title, post %></li>
<% end %>
</ul>
class Tag < ActiveRecord::Base
has_many :post_tags
has_many :posts, through: :post_tags
end
<!-- app/views/tags/_form.html.erb -->
<%= form_for @tag do |f| %>
<div class='form-group'>
<div class='col-md-8'>
<%= f.label :name, class: 'form-label' %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class='col-md-4'>
<%= f.submit 'Submit', class: 'btn btn-primary input-without-label' %>
</div>
</div>
<% end %>
<!-- app/views/tags/index.html.erb -->
<div class='row'>
<div class='col-md-6'>
<h1>Tags</h1>
<ul>
<% @tags.each do |tag| %>
<li><%= tag.name %></li>
<% end %>
</ul>
</div>
<div class='col-md-6'>
<h1>New Tag</h1>
<%= render 'form' %>
</div>
</div>
class TagsController < ApplicationController
respond_to :html
def index
@tags = Tag.all
@tag = Tag.new
end
def show
@tag = Tag.find(params[:id])
end
def create
@tag = Tag.create(safe_params)
respond_with @tag, location: tags_url, action: :index
end
private
def safe_params
params.require(:tag).permit(:name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment