2012-08-02 99 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