タイムシート インデックスに user.name を表示するアプリに問題があります (現在のユーザーのタイムシートだけでなく、すべてのタイムシートを表示します)
ユーザーモデル:
# Table name: timesheets
#
# id :integer not null, primary key
# user_id :integer
# starting :datetime
# ending :datetime
# approved :boolean
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :timesheets
....
end
タイムシート モデル (標準の user_id は外部キー)
# Table name: timesheets
#
# id :integer not null, primary key
# user_id :integer
# starting :datetime
# ending :datetime
# approved :boolean
# created_at :datetime not null
# updated_at :datetime not null
#
class Timesheet < ActiveRecord::Base
attr_accessible :starting, :ending
belongs_to :user
validates :user_id, presence: true
validates :starting, presence: true
validates :ending, presence: true
validate :end_after_start
...
end
私のタイムシートコントローラーからのインデックス定義:
def index
@timesheets = Timesheet.paginate(page: params[:page], per_page: 10)
end
最後に、user_id が表示されている index.html.erb ファイルですが、代わりにユーザー名が必要です。これは管理者がタイムシートを承認するためのものであるため、サインインしたユーザーではなく、すべてのタイムシートを表示して必要なものを承認したい管理者であることを忘れないでください。
<ul class="timesheets">
<% @timesheets.each do |timesheet| %>
<NOBR>
<li>
<%= timesheet.user_id %>
<%= timesheet.starting.strftime("[ %^a %^b %e, %Y - %l:%M %p ] - ") %><%= timesheet.ending.strftime("[ %^a %^b %e, %Y - %l:%M %p ]") %> <%= (timesheet.ending - timesheet.starting)/3600 %> Hours
<% if current_user.admin? %>
|
<% if timesheet.approved? %>
<%= link_to "Un-Approve", { action: :unapprove, id: timesheet.id }, method: :put %>
<% else %>
<%= link_to "Approve", { action: :approve, id: timesheet.id }, method: :put %>
<% end %>
<% end %>
</li>
</NOBR>
<% end %>
</ul>
<%= will_paginate %>
これは私の最初の質問なので、すべての正しいプロトコルに従っていることを願っています。