Skip to content

Instantly share code, notes, and snippets.

@pam-
Last active August 29, 2015 14:04
Show Gist options
  • Save pam-/71c5c6fe6fad03e5dcf8 to your computer and use it in GitHub Desktop.
Save pam-/71c5c6fe6fad03e5dcf8 to your computer and use it in GitHub Desktop.
categories struggle
class Categorization < ActiveRecord::Base
belongs_to :post
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :categorizations
has_many :posts, through: :categorizations
end
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :categorizations
end
<% if signed_in? && current_user?(@user) %>
<%= form_for(@post) do |f| %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= render 'shared/error_messages', object: f.object %>
<div class="title-area">
<%= f.text_field :title, placeholder: "Enter title..." %>
</div>
<div class="text-area">
<%= f.text_area :content, placeholder: "Compose new post..." %>
</div>
<%= f.submit "Story", class: "post-button", id: "left" %>
<%= f.submit "Question", class: "post-button", id: "right" %>
<% end %>
<% end %>
def create
@post = current_user.posts.build(post_params)
@story = Category.find(1)
@question = Category.find(2)
if params[:commit] == 'Story'
@post.category = @story
@story.posts << @post
elsif params[:commit] == 'Question'
@post.category = @question
@question.posts << @post
end
if @post.save
flash[:success] = "Post created!"
redirect_to @post
else
flash.now[:error] = "Couldn't post!"
redirect_to :back
end
end
def destroy
Post.find(params[:id]).destroy
flash[:success] = "Successfully deleted!"
redirect_to current_user
end
create_table "categories", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "posts", force: true do |t|
t.string "content"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "title"
t.integer "category_id"
end
class User < ActiveRecord::Base
def show
@user = User.find(params[:id])
@post = current_user.posts.build if signed_in?
@posts = @user.posts.paginate(page: params[:post_page], per_page: 5)
@feed_items = @user.followed_posts.paginate(page: params[:feed_item_page], per_page: 2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment