Reputation: 55293
My Post model has a boolean called "published":
schema.rb:*
create_table "posts", :force => true do |t|
t.string "title"
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "published", :default => false
end
If it is true, the post is considered published, and it appears in the published section of the user's profile, and if it is false it appears in the draft section (sorry, but I'm not sure how to deal with the code duplication).
posts_controller.rb:
class PostsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
def index
@posts = Post.all
end
etc...
users_controller.rb:
class UsersController < ApplicationController
def index
@users = User.all
end
def show
@user = User.find(params[:id])
@posts = @user.posts
end
etc...
_shared/posts.thml:
<h2>Published</h2>
<% @posts.each do |post| %>
<% if post.published? %>
<div id="post-<%= post.id %>" class="post">
<%= image_tag post.image.url(:medium) %>
<h2 class="post-title"><%= link_to post.title, post %></h2>
<%= link_to post.user.email, post.user %>
<p><%= post.content %></p>
</div>
<% end %>
<% end %>
<h2>Draft</h2>
<% @posts.each do |post| %>
<% if not post.published? %>
<div id="post-<%= post.id %>" class="post">
<%= image_tag post.image.url(:medium) %>
<h2 class="post-title"><%= link_to post.title, post %></h2>
<%= link_to post.user.email, post.user %>
<p><%= post.content %></p>
</div>
<% end %>
<% end %>
Example:
users.html.erb:
<h2>User Show</h2>
<%= image_tag @user.avatar.url(:medium) %>
<span>Email: <%= @user.email %></span><br />
<span>ID: <%= @user.id %></span><br />
<%= render 'shared/posts' %>
index.html.erb
<%= render 'shared/posts' %>
(I know the views are a little bit messy. I think later on I will just show a text saying 'draft' beside each post draft).
The problem is that the posts will be sorted by their created_at
date. I want them to be sorted by a published_at
date in the post's index page (I think that makes more sense) .
I know how to add the published_at
t.datetime
field via migration. But I'm not sure what code to use for the logic in the published_at
field.
Any suggestions?
(by the way, what sounds more correct? 'published_at
' or 'published_on
'?)
Upvotes: 3
Views: 3017
Reputation: 6036
add in your table published_at
(datetime) field. and when you publish then update the published_at field and then when you fetch data use order('published_at Desc')
@posts = @users.posts.order('published_at DESC')
logic like in your controller
if params[:post][:published] == true # whatever you got in controller rather than params[:post][:published]
@post.published_at = Time.now
else
@post.published_at = ""
end
that means, when published
value is true then above code perform in your controller and in your published_at field add value Time.now and if value of published is false then above code is not perform so published_at
field have blank value.
or, by using model like
before_save :published_post
def published_post
if self.published == true
self.published_at = Time.now
end
end
Upvotes: 4
Reputation: 434835
You should be able to adjust your controller:
def show
@user = User.find(params[:id])
posts = @user.posts.order('published_at').group_by(&:published)
@published = posts[true]
@drafts = posts[false]
end
Then you can use @published
and @drafts
to produce your lists. If you want the most recently published ones first then:
posts = @user.posts.order('published_at desc').group_by(&:published)
Upvotes: 1