我終於弄明白了,並得到它的工作,所以我會分享我的答案。大道具來幫忙!這有點亂,可能不是我應該這樣做的正確方式,但我的第一個目標是讓它工作並學習它如何它沿途。現在我正在整理並學習最佳實踐。
路線
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