Ruby on rails ruby打印数据库内容

Ruby on rails ruby打印数据库内容,ruby-on-rails,ruby,Ruby On Rails,Ruby,我是Ruby新手。 我必须使用教程呈现数据库结构,我找到了合适的代码: 文件index.html.erb: <h1>Listing tasks</h1> <table> <tr> <th>Title</th> <th>Content</th> <th>Date From</th> <th>Date To</th>

我是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 %>
<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 %>
这可以在“不工作”链接中使用,将数据传递到“Show Date”(Show_Date.html.erb),但我总是会出错。问题在于正确取消show_date.html.erb,代码我可以自己编写:)

任务控制器中的Urmas

添加一个方法

def index_date

@tasks = Task.all

end
并创建一个索引_date.html.erb write

<% @tasks.each do |task| %>   
<tr>     <td><%= task.datefrom %></td>
          <td><%= link_to 'Edit', {:action => "index", :date => task.datefrom} %></td>
</tr>
<%end%>
这将列出与所选日期匹配的任务,此后所有任务都将与现在相似。

找到了解决方案

有必要添加到routes.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)调用它


所有进一步的处理都已在showdate.html.erb中

希望这对别人有帮助


我是最棒的!!!!xD

编辑了一个问题。这并不是我想要的require@Urmas雷宾斯基:我不能理解你的问题。您是否无法单击链接并访问show_date.html.erb页面?如果错误显示没有路由匹配,则需要将-get“tasks/show_date”添加到路由中。rbAdded:to=>“tasks/show_date”对于root、get/home/urmas/sites/taskmanager4/config/routes。rb:56:语法错误,意外的tASSOC,期望kEND:to=>'tasks/show_date'@Urmas Repinski如果你需要将_date显示为root,你需要这样做-root:to=>'tasks#show_date'No。我想要一些不同的东西作为root。
@tasks = Task.find_all_by_datefrom(#{params[:date])
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 
<td><%= link_to 'Show Date', showdate_path(task.id)  %></td>