2012-08-02 101 views
0

我是Ruby中的新手。 我要呈現的數據庫結構,使用教程中,我找到了適合代碼:紅寶石打印數據庫內容

文件index.html.erb:

<h1>Listing tasks</h1> 

<table> 
    <tr> 
    <th>Title</th> 
    <th>Content</th> 
    <th>Date From</th> 
    <th>Date To</th> 
    <th></th> 
    <th></th> 
    <th></th> 
    </tr> 

<% @tasks.each do |task| %> 
    <tr> 
    <td><%= task.title %></td> 
    <td><%= task.content %></td> 
    <td><%= task.datefrom %></td> 
    <td><%= task.dateto %></td> 
    <td><%= link_to 'Show', task %></td> 
    <td><%= link_to 'Edit', edit_task_path(task) %></td> 
    <td><%= link_to 'Destroy', task, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New Task', new_task_path %> 

現在我想組任務由值「從日期」,大概意思在index.html.erb我應該只打印日期,並點擊日期我應該調用另一個HTML文件,將按選定日期呈現任務。

這可以像

文件index.html.erb:

<h1>Listing dates</h1> 

<table> 
    <tr> 
    <th>Date From</th> 

<% @tasks.each do |task| %> 
    <tr> 
    <td><%= !!! IF DATE NOT PRINTED THEN PTINT IT task.datefrom %></td> 
    <td><%= link_to 'Edit', edit_date_path(task, date) %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New Task', new_task_path %> 

凡edit_date_path(任務,日期)應該是指我的index_date.html.erb,在那裏我可以選擇日期並根據選定的日期打印任務。

也許我可以得到一些建議,有些人可以幫我解決這個問題,因爲任務不應該很困難,但否則我會浪費很多時間搜索。

謝謝, Urmas。

編輯問題。

這對此有所幫助。我現在所做的,我改變index.html.erb到

<h1>Listing dates</h1> 

<table> 
    <tr> 
    <th>Date To</th> 
    <th></th> 
    </tr> 

<% dates = Array.new %> 
<% @tasks.each do |task| %> 

<% if (!dates.include?(task.dateto.strftime("%m.%d.%Y"))) then 
    dates.push task.dateto.strftime("%m.%d.%Y") 
%> 

    <tr> 
    <td><%= task.datefrom.strftime("%m.%d.%Y") %></td> 
    <td><%= link_to 'Show Date', {:action => "index", :date => task.dateto} %></td> <-- This does not work 
    </tr> 

<% 
    end 
%> 

<% end %> 
</table> 

<br /> 

<%= link_to 'New Task', new_task_path %> 

在哪裏「顯示日期」鏈接鏈接提出,要show_date.html.erb,那裏是表示在輸入日期已過記錄正確的代碼。

我在控制器還方法添加的所有

def show_date 
    @tasks = Task.find(params[:dateto]) 
    @dateto = :dateto 

    respond_to do |format| 
     format.html # showdate.html.erb 
     format.json { render :json => @tasks } 
    end 
end 

,可以在不工作的鏈接使用,將數據傳遞到「顯示日期」(show_date.html.erb),但我得到的錯誤時間。問題是與正確的調用關閉show_date.html.erb,代碼,我就能自己:)

Urmas

回答

0

寫在任務控制器:

添加一個方法

def index_date 

@tasks = Task.all 

end 

並創建index_date.html.erb寫入

<% @tasks.each do |task| %> 
<tr>  <td><%= task.datefrom %></td> 
      <td><%= link_to 'Edit', {:action => "index", :date => task.datefrom} %></td> 
</tr> 
<%end%> 

並在索引方法d控制器改變

@tasks = Task.all

@tasks = Task.find_all_by_datefrom(#{params[:date]) 

這將列出與所選的日期的任務,一切都將是類似的,因爲它是現在其後。

+0

編輯了一個問題。這不正是我所需要的 – 2012-08-02 18:11:45

+0

@Urmas Repinski我無法理解你的問題。您是否無法點擊鏈接並訪問show_date.html.erb頁面?如果錯誤說沒有路由匹配,那麼你需要添加 - 獲取「tasks/show_date」到你的routes.rb – user1455116 2012-08-02 18:28:43

+0

增加:to =>'tasks/show_date'爲root,get/home/urmas/sites/taskmanager4/config /routes.rb:56:語法錯誤,意外tASSOC,期待kEND :to =>'tasks/show_date' – 2012-08-02 18:34:45

0

找到了解決方案。

有必要添加到路線。RB

resources :tasks do 
    member do 
    get 'showdate' 
    end 
    get "home/index" 
end 

添加相應的控制器處理

def showdate 
    @task = Task.find(params[:id]) 
    @tasks = Task.find(:all) 

    respond_to do |format| 
    format.html # showdate.html.erb 
    format.json { render :json => @tasks } 
    end 
end 

並使用(index.html.erb)

<td><%= link_to 'Show Date', showdate_path(task.id) %></td> 

所有進一步的處理已經在showdate.html.erb調用它。

希望這有助於某人。

我是最棒的! xD