我是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
编辑了一个问题。这不正是我所需要的 – 2012-08-02 18:11:45
@Urmas Repinski我无法理解你的问题。您是否无法点击链接并访问show_date.html.erb页面?如果错误说没有路由匹配,那么你需要添加 - 获取“tasks/show_date”到你的routes.rb – user1455116 2012-08-02 18:28:43
增加: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