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(task, date) は、index_date.html.erb を参照する必要があります。ここで、選択した日付を取得し、選択した日付に従ってタスクを印刷できます。

たぶん私は提案を得ることができます.fomebodyがそれを手伝ってくれるとはるかに簡単になります.タスクはそれほど難しくないはずですが、そうでなければ私はグーグルでかなりの時間を無駄にすることができます.

ありがとう、ウルマス。

質問の編集。

これは少し役に立ちました。私が今やったこと、私は 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」リンクは、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 を正しく呼び出すことです。コードは自分で書くことができます:)

ウルマス

4

2 に答える 2

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

コントローラーのインデックスメソッドの変更

@tasks = Task.all

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

これにより、選択した日付に一致するタスクが一覧表示され、その後はすべてが同様になります。

于 2012-08-02T16:30:26.610 に答える
0

解決策を見つけました。

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) を使用して呼び出す

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

以降のすべての処理は、すでに showdate.html.erb にあります。

これが誰かに役立つことを願っています。

私は最高です!!!! xD

于 2012-08-03T07:13:42.320 に答える