如何按日期顺序限制方法(3 次迭代)?
How to Limit Method (3 iterations) by Date Order?
我们如何将用户未完成的目标(也就是他尚未完成的目标)按降序排列到即将到来的 3 个目标中?
例如:
Run a marathon 3/1/15*
Create an app 4/1/15*
Buy a house 5/15/15*
Win political office 1/1/16
Marry a wife who can cook & code 2/2/16
*How to show only the first 3?
目的是在侧边栏中显示此代码以供用户参考。
观看次数,进球,_upcoming.html.erb
<table>
<% @unaccomplished_goals.each do |unaccomplished| %>
<tr>
<td>
<b><%= link_to edit_goal_path(unaccomplished) do %>
<%= unaccomplished.name %>
<% end %></b></td>
<td>
<%= unaccomplished.deadline.strftime("%m-%d-%Y") %>
</td>
</tr>
<% end %>
</table>
目标控制器
class GoalsController < ApplicationController
before_action :set_goal, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :destroy]
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
else
@goals = Goal.all.order("deadline")
@accomplished_goals = current_user.goals.accomplished
@unaccomplished_goals = current_user.goals.unaccomplished
end
end
def show
end
def new
@goal = current_user.goals.build
end
def edit
end
def create
@goal = current_user.goals.build(goal_params)
if @goal.save
redirect_to goals_url, notice: 'Goal was successfully created'
else
@feed_items = []
render 'pages/home'
end
end
def update
if @goal.update(goal_params)
redirect_to goals_url, notice: 'Goal was successfully updated'
else
render action: 'edit'
end
end
def destroy
@goal.destroy
redirect_to goals_url
end
private
def set_goal
@goal = Goal.find(params[:id])
end
def correct_user
@goal = current_user.goals.find_by(id: params[:id])
redirect_to goals_path, notice: "Not authorized to edit this goal" if @goal.nil?
end
def goal_params
params.require(:goal).permit(:name, :deadline, :accomplished, :tag_list)
end
end
谢谢=]
# class Goal
scope :top_3, -> do
order("deadline DESC").
limit(3)
end
# GoalsController
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
else
@goals = Goal.all.order("deadline")
@accomplished_goals = current_user.goals.accomplished
@unaccomplished_goals = current_user.goals.unaccomplished
@top_3_goals = @unaccomplished_goals.top_3
end
# views/goals/_upcoming.html.erb
<table>
<% @top_3_goals.each do |goal| %>
<tr>
<td>
<strong>
<%= link_to goal.name, edit_goal_path(goal) %>
</strong>
</td>
<td>
<%= goal.deadline.strftime("%m-%d-%Y") %>
</td>
</tr>
<% end %>
</table>
我们如何将用户未完成的目标(也就是他尚未完成的目标)按降序排列到即将到来的 3 个目标中?
例如:
Run a marathon 3/1/15*
Create an app 4/1/15*
Buy a house 5/15/15*
Win political office 1/1/16
Marry a wife who can cook & code 2/2/16
*How to show only the first 3?
目的是在侧边栏中显示此代码以供用户参考。
观看次数,进球,_upcoming.html.erb
<table>
<% @unaccomplished_goals.each do |unaccomplished| %>
<tr>
<td>
<b><%= link_to edit_goal_path(unaccomplished) do %>
<%= unaccomplished.name %>
<% end %></b></td>
<td>
<%= unaccomplished.deadline.strftime("%m-%d-%Y") %>
</td>
</tr>
<% end %>
</table>
目标控制器
class GoalsController < ApplicationController
before_action :set_goal, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:create, :destroy]
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
else
@goals = Goal.all.order("deadline")
@accomplished_goals = current_user.goals.accomplished
@unaccomplished_goals = current_user.goals.unaccomplished
end
end
def show
end
def new
@goal = current_user.goals.build
end
def edit
end
def create
@goal = current_user.goals.build(goal_params)
if @goal.save
redirect_to goals_url, notice: 'Goal was successfully created'
else
@feed_items = []
render 'pages/home'
end
end
def update
if @goal.update(goal_params)
redirect_to goals_url, notice: 'Goal was successfully updated'
else
render action: 'edit'
end
end
def destroy
@goal.destroy
redirect_to goals_url
end
private
def set_goal
@goal = Goal.find(params[:id])
end
def correct_user
@goal = current_user.goals.find_by(id: params[:id])
redirect_to goals_path, notice: "Not authorized to edit this goal" if @goal.nil?
end
def goal_params
params.require(:goal).permit(:name, :deadline, :accomplished, :tag_list)
end
end
谢谢=]
# class Goal
scope :top_3, -> do
order("deadline DESC").
limit(3)
end
# GoalsController
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
else
@goals = Goal.all.order("deadline")
@accomplished_goals = current_user.goals.accomplished
@unaccomplished_goals = current_user.goals.unaccomplished
@top_3_goals = @unaccomplished_goals.top_3
end
# views/goals/_upcoming.html.erb
<table>
<% @top_3_goals.each do |goal| %>
<tr>
<td>
<strong>
<%= link_to goal.name, edit_goal_path(goal) %>
</strong>
</td>
<td>
<%= goal.deadline.strftime("%m-%d-%Y") %>
</td>
</tr>
<% end %>
</table>