我终于弄明白了,并得到它的工作,所以我会分享我的答案。大道具来帮忙!这有点乱,可能不是我应该这样做的正确方式,但我的第一个目标是让它工作并学习它如何它沿途。现在我正在整理并学习最佳实践。
路线
Rails.application.routes.draw do
resources :business_dates, except: :show
get '/business_dates/:year/:month/:day', to: 'business_dates#edit_date', as: 'edit_date'
get '/business_dates/:year/:month', to: 'business_dates#by_days', as: 'by_days'
get '/business_dates/:year', to: 'business_dates#by_months', as: 'by_months'
delete '/business_dates/:year/:month/:day', to: 'business_dates#delete_date', as: 'delete_date'
控制器
class BusinessDatesController < ApplicationController
def index
@business_dates = BusinessDate.all
@years = BusinessDate.pluck(:calendar_date).map{|x| x.year}.uniq
end
def new
@date = BusinessDate.new
end
def by_months
@months = BusinessDate.where("strftime('%Y', calendar_date) = ?", params[:year])
@months = @months.pluck(:calendar_date).map{|x| x.strftime('%m')}.uniq
end
def by_days
@days = BusinessDate.where("cast(strftime('%Y', calendar_date) as int) = ? AND cast(strftime('%m', calendar_date) as int) = ?", params[:year], params[:month])
end
def edit_date
set_business_date
end
def create
@date = BusinessDate.new(date_params)
if @date.valid?
@date.save
redirect_to by_days_path(@date.calendar_date.year, "%02d" % @date.calendar_date.month)
else
flash.now[:alert] = "New business date could not be saved"
render action: "new"
end
end
def update
#set_business_date
@date = BusinessDate.find(params[:id])
@date.update(date_params)
redirect_to by_days_path(@date.calendar_date.year, "%02d" % @date.calendar_date.month)
end
def delete_date
set_business_date
@date.destroy
redirect_to by_days_path(params[:year], params[:month])
end
private
def set_business_date
@date = BusinessDate.where("cast(strftime('%Y', calendar_date) as int) = ? AND cast(strftime('%m', calendar_date) as int) = ? AND cast(strftime('%d', calendar_date) as int) = ?", params[:year], params[:month], params[:day]).first
end
def date_params
params.require(:business_date).permit(:calendar_date, :seasonality)
end
end
index.html.erb
<h1>Business Dates by Year</h1>
<table>
<tr>
<th>Calendar Date</th>
</tr>
<% @years.sort.each do |year| %>
<tr>
<td><%= link_to year, business_date_path(year) %></td>
</tr>
<% end %>
</table>
<br>
<%= link_to 'Create New Business Date', new_business_date_path %>
by_months.html.erb
<h1><%= params[:year] %></h1>
<table>
<tr>
<th>Months</th>
</tr>
<% @months.sort.each do |month| %>
<tr>
<td><%= link_to Date::MONTHNAMES[month.to_i], by_days_path(params[:year], month) %></td>
</tr>
<% end %>
</table>
<br>
<%= link_to 'Create New Business Date', new_business_date_path %>
<br>
<%= link_to 'Back to Years', business_dates_path %>
by_days.html.erb
<h1><%= Date::MONTHNAMES[params[:month].to_i] %>, <%= params[:year] %> <%= params[:id] %></h1>
<table>
<tr>
<th>Date</th>
<th>Seasonality</th>
<th>ID</th>
</tr>
<% @days.sort.each do |day| %>
<tr>
<td><%= day.calendar_date.day %></td>
<td><%= day.seasonality %></td>
<% %>
<td><%= day.id %></td>
<td><%= link_to 'Edit', edit_date_path(day.calendar_date.year, "%02d" % day.calendar_date.month, day.calendar_date.day) %></td>
<td><%= link_to 'Delete', delete_date_path(day.calendar_date.year, "%02d" % day.calendar_date.month, day.calendar_date.day), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br>
<%= link_to 'Create New Business Date', new_business_date_path %>
<br>
<%= link_to 'Back to Months', by_months_path %>
<br>
<%= link_to 'Back to Years', business_dates_path %>
这是所有工作正常,但我希望我能想出我的:ID发布。不知道如何使用:year /:month /:day url约定通过:id查找记录。这个应用程序不是问题,考虑到应该永远不会超过同一天的一个,但它会有帮助,我相信它会减少必须通过params [:year]搜索记录, [:月]和[:日]。这件事是一个神圣的恐怖,但我肯定了解了数组,哈希,符号,属性,模型,方法和实例变量之间的差异!
我对这是如何工作有点困惑。 因此,如果每年都有一个记录(2005年,2006年,2007年等),并且我点击2007年,它应该链接到business_dates /:year。但是由于我没有缩小完整日期,所以只有在calendar_date列中匹配2007年的所有记录,这对我来说无法实现。我需要从一开始就有预定的日期。对? 我重申,我是新的。 – backsliders
你可以有不同的途径: 'business_dates /:年/月/:day' 'business_dates /:年/:month' 'business_dates /:year' 年页列出了所有个月链接。月份页面将所有日期列为链接,因此您可以查看完整日期页面。 我刚刚给你一个方法来创建自定义路线 - 你可以使用它们,但你想:) – arunt
我欣赏不断的帮助。我仍然对如何在控制器中设置它感到困惑。我一直在为此工作近一个星期,而且我一直在打击死胡同。 – backsliders