ruby on rails - How to show user.name associated with timesheets? -
i having issues app displaying user.name in timesheets index (shows timesheets, not current user timesheets)
user model:
# 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
timesheet model (standard user_id foreign key)
# 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
the index def timesheets controller:
def index @timesheets = timesheet.paginate(page: params[:page], per_page: 10) end
and index.html.erb file has user_id displayed, want user name instead. remember admin approve timesheets wont signed in user, rather admin wants see timesheets , approve ones want.
<ul class="timesheets"> <% @timesheets.each |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 %>
many help, first question asked hope have followed correct protocols.
if have association user model can user model calling
timesheet.user
in case of problem
<ul class="timesheets"> <% @timesheets.each |timesheet| %> <nobr> <li> <%= timesheet.user.name %> <%= 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 %>
Comments
Post a Comment