2017-08-12 16 views
1

私は非常にレールに新しく、何か問題があります。私は2つのテーブル、calendar_dateと季節性(あなたは季節性を無視することができます)から成っているBusinessDatesと呼ばれるモデルを持っています。私が達成しようとしているのは、フォルダのように簡単に移動できることです。レール/(年)/(月)/(日)別ページ

私はgoogle-fooを数日間動かしましたが、フレンドIDとのリンクとしてインデックスに各ユニークな年のリストを順番に表示することができました。ここから私はそれをクリックして、フレンドリーIDとのリンクとして、その特定の年の各ユニークな月のリストを順番に表示する新しいビューにリンクさせたいと思います。それで、(あなたが推測できるように)選択された年の選択された月のすべての日のリストを別の新しいビューに表示させます。私は、business_dates/2016/5/21または言い換えればbusiness_dates /(年)/(月)/(日)のURLにします。

私の問題:ここからどこに行くのか分かりません。私は毎年の月と日を別々のモデル(避けたい)を作ることなく、またはちょうどそこに着くためにルーベゴールドバーグマシンのように見えるものを作ることなく、第2レベルの非静的URLを作ることについての情報を見つけるように見えない各ページの表示なしで(完全な日付をURLに入力するだけで済みます)。

非常に迷っている初心者を助けてください!

コントローラー:

<table> 
    <tr> 
     <th>Year</th> 
    </tr> 
    <% @years.sort.each do |year| %> 
    <tr> 
     <td><%= link_to year, business_date_path(year) %></td> 
    </tr> 
<% end %> 
</table> 

答えて

0

私は最終的にそれを理解して動作させたので、私は答えを共有します。助けを狙う大きな小道具!ちょっと厄介で、おそらく私がこれをやるべき正しい方法ではないが、私の最初の目標はそれを働かせて学ぶことだった。howそれは道に沿って働く。今私は整頓してベストプラクティスを学ぶことを目指しています。

ルート

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 %> 

<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 %> 

by_days.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_months.html.erbそれは、すべての作業罰金だが、私は私が私を見つけ出すことがしたい:ID発行を。 :year /:month /:day URLの規則を使っているときに:idでレコードを見つける方法がわからない。それは同じ日付の1つ以上であるべきではないことを考慮して、このアプリの問題ではありませんが、それは有用であり、私はそれがparams [:年]、 [:月]、[:日]です。このことは神聖な恐怖でしたが、配列、ハッシュ、シンボル、属性、モデル、メソッド、インスタンス変数の違いについては、多くのことを学びました。

0

index.erb.html

def index 
    @years = BusinessDate.pluck(:calendar_date).map{|x| x.year}.uniq 
end 

カスタムルートを作成することができます。あなたが使用している正確なコントローラーの動作などを知らないので、私はあなたに一般的な答えを与えます。あなたのようなルートは、(BusinessDatesControllerの:show_full_dateアクションをヒットする)ことができます:あなたが好きそれにリンクすることができ

get 'business_dates/:year/:month/:day', to: 'business_dates#show_full_date' 

(実行すくいルートが正しいパスをチェックする):

<%= link_to your_date, full_date_business_dates_path('1985','5','21') %> 

ここで理解することは重要なことは、ということですパスヘルパーは最後に引数を取ることができるメソッドです。それが受け入れるものはroutes.rbで定義されています。つまり、私たちの場合、それは:年、月、日のパラメータです。

このリンクをクリックして:show_full_dateアクションを実行すると、params [:year]、params [:month]、params [:day]を使用して年、月、日を抽出し、行う。同様に、年または月だけのルートを定義することができます。お役に立てれば。

EDIT:パス定義にas:オプションを指定して、特定の名前をas: 'my_funky_name'のように指定することもできます。

また、このようなカスタムルートを最小限に抑える必要があります。それが必要なときは、それをやりなさい。さもなければデフォルトに固執してください。

+0

これはどのように動作するのか少し混乱します。 レコードがある毎年(2005年、2006年、2007年など)、それを表示すると、2007年をクリックするとbusiness_dates /:年にリンクされます。しかし、私は完全な日付を絞り込んでいないので、2007年のcalendar_date列に一致するすべてのレコードのみがこれを達成するのに役立ちません。私は最初から予定された日付を持つ必要があります。右? 私は繰り返し、私は新しいです。 – backsliders

+0

あなたは別のルートを持つことができます。 'business_dates /:年/:月/:day' ' business_dates /:年/:year' 年のページリストのすべての月のように: 'business_datesを/ month'リンク。月のページにはすべての日付がリンクとしてリストされているため、完全な日付のページが表示されます。 私はちょうどあなたにカスタムルートを作成する方法を与えました - あなたはそれらを使用することができます:) – arunt

+0

私は定数のヘルプに感謝します。私はコントローラでこれをどのように設定するかについて、まだ混乱しています。私はこの1週間しか作業していませんでした。 – backsliders