Dear everyone,
I''m sorry if this email appears to be long as I include the code to see
if
it''s easier to understand my question.
My code is certainly not pretty so if there is any DRY suggestions or
improvement on the code please let me know also.
There is a list_week page that show work times (check in, check out) for
an employee during the 7 days period. If there is no work times in a
specific date, it will create one in advance before a person see the view.
That helps me to set up the next page, edit_time_weekly for a mulitple edit.
The code for controller for list_week page is:
#start the code
def list_week
current_user_id = params[:id]
@an_user = User.find(current_user_id)
unless @an_user.id == session[:user].id
flash[:notice] = "User not found"
redirect_to(:controller=>"account", :action=>"login")
end
now = Time.now
@t1 = now.at_beginning_of_week
@t2 = @t1+(60*60*24)
@t3 = @t1+(60*60*24*2)
@t4 = @t1+(60*60*24*3)
@t5 = @t1+(60*60*24*4)
@t6 = @t1+(60*60*24*5)
@t7 = @t1+(60*60*24*6)
@t8 = @t1+(60*60*24*7)
@date_range_from = @t1.strftime("%y-%m-%d")
@date_range_to = @t7.strftime("%y-%m-%d")
@t1_sql = @t1.strftime("%y-%m-%d")
@t2_sql = @t2.strftime("%y-%m-%d")
@t3_sql = @t3.strftime("%y-%m-%d")
@t4_sql = @t4.strftime("%y-%m-%d")
@t5_sql = @t5.strftime("%y-%m-%d")
@t6_sql = @t6.strftime("%y-%m-%d")
@t7_sql = @t7.strftime("%y-%m-%d")
@t8_sql= @t8.strftime("%y-%m-%d")
@work_time= Array.new(6)
@work_time[0] = WorkTime.find(:first, :conditions=>["user_id=? and
check_in>=''#{@t1_sql}''and check_in
<=''#{@t2_sql}''",current_user_id])
@work_time[1] = WorkTime.find(:first, :conditions=>["user_id=? and
check_in>=''#{@t2_sql}''and check_in
<=''#{@t3_sql}''",current_user_id])
@work_time[2] = WorkTime.find(:first, :conditions=>["user_id=? and
check_in>=''#{@t3_sql}''and check_in
<=''#{@t4_sql}''",current_user_id])
@work_time[3] = WorkTime.find(:first, :conditions=>["user_id=? and
check_in>=''#{@t4_sql}''and check_in
<=''#{@t5_sql}''",current_user_id])
@work_time[4] = WorkTime.find(:first, :conditions=>["user_id=? and
check_in>=''#{@t5_sql}''and check_in
<=''#{@t6_sql}''",current_user_id])
@work_time[5] = WorkTime.find(:first, :conditions=>["user_id=? and
check_in>=''#{@t6_sql}''and check_in
<=''#{@t7_sql}''",current_user_id])
@work_time[6] = WorkTime.find(:first, :conditions=>["user_id=? and
check_in>=''#{@t7_sql}''and check_in
<=''#{@t8_sql}''",current_user_id])
i=0
while i < @work_time.length
if @work_time[i] == nil
WorkTime.new do |w|
w.check_in = now.at_beginning_of_week+(60*60*24)*i
w.check_out = now.at_beginning_of_week+(60*60*24)*i
w.user_id = "#{@an_user.id}"
w.lunch = "01:00:00"
w.save
@work_time[i] = w
end
end
i+=1
end
end
#below is the code for view list_weekly
<%=start_form_tag(:action=>''edit_time_weekly'',
:id=>@an_user.id) %>
<% weekly_period = [@t1, @t2, @t3, @t4, @t5, @t6, @t7] %>
<table cellpadding="0" cellspacing="0">
<tr class="main_title">
<td>Date</td>
<td>Week</td>
<td>Check in</td>
<td>Check out</td>
<td>Lunch</td>
</tr>
<% i = 0 %>
<% for week_day in weekly_period %>
<tr>
<td class="table_data"><%=
week_day.strftime("%m-%d")%></td>
<td class="table_data"><%=
week_day.strftime("%a")%></td>
<% if @work_time[i].check_in.strftime("%H-%M")=="00-00"
%>
<td class="table_data">--</td>
<td class="table_data">--</td>
<td class="table_data">1</td>
<% else %>
<td
class="table_data"><%=@work_time[i].check_in.strftime("%H:%M
at
%d")%></td>
<td
class="table_data"><%=@work_time[i].check_out.strftime("%H:%M
at
%d")%></td>
<td class="table_data">1</td>
<% end %>
</tr>
<% i+=1 %>
<% end %>
</table>
<p style="margin-top: 5px"><%=submit_tag "Edit"
%></p>
<%=end_form_tag %>
# end of code
The list_week works, my problem is when I try to do the edit_time_weekly.
The code for edit_time_weekly in controller:
def edit_time_weekly
current_user_id = params[:id]
@an_user = User.find(current_user_id)
now = Time.now
@t1 = now.at_beginning_of_week
@t7 = @t1+(60*60*24*6)
@t8 = @t1+(60*60*24*7)
@date_range_from = @t1.strftime("%y-%m-%d")
@date_range_to = @t8.strftime("%y-%m-%d")
@work_times = WorkTime.find(:all, :conditions=>["user_id=? and
check_in>=''#{@date_range_from}''and check_in <
=''#{@date_range_to}''",current_user_id])
end
# end of code
My code for view:
<%=start_form_tag(:action=>''update_time_weekly'',
:id=>@an_user.id) %>
<table class="dash_box" cellpadding="0"
cellspacing="0">work
<tr class="main_title">
<td>Date</td>
<td>Week</td>
<td>Check in</td>
<td>Check out</td>
<td>Lunch</td>
</tr>
<% for work_time in @work_times%>
<tr>
<td
class="table_data"><%=work_time.check_in.strftime("%m-%d")%></td>
<td
class="table_data"><%=work_time.check_in.strftime("%a")%></td>
<td class="table_data"><%=datetime_select(:work_time,
:check_in,
:minute_step=>15)%></td>
<%=hidden_field_tag("work_time["+work_time.id.to_s+"][check_in]",
"")%>
<td class="table_data"><%=datetime_select(:work_time,
"check_out",
:minute_step=>15)%></td>
<%=hidden_field_tag("work_time["+work_time.id.to_s+"][check_out]",
"")%>
<td class="table_data">1:00</td>
</tr>
<% end %>
</table>
<p style="margin-top: 5px"><%=submit_tag "Update"
%></p>
<%=end_form_tag %>
<h4><%=button_to ''logout'',
:controller=>''account'', :action =>
''logout''
%></h4>
#end of code for view
The problem happens in datetime_select. using the code above, the selectable
datetime is not from the database, instead it shows only the current time.
Is there a way I can do such that I can make the existing datetime show on
the view and allow a mulitple update?
Thank you very much for your time.
Sammy
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails