2017-05-18 24 views
0

私は現在、週単位の記入フォームを持っています。どのように私は同じページに別のタイムエントリフォームを持つことができますか?これらの2つの書式を別々の記録として別々に提出する必要があります。どんな助けもありがとう。ここでレール上の同じページにフォームタグ付きの2つの提出書類

は私のコードです: -

show_weeks.html.erb

<div class="table-responisve> 
    <% if @dates != nil %> 

<table class="table-bordered"> 
<thead> 
    <tr> 
    <% @dates.each do |date| %> 
     <th><%=date.to_s+","+date.strftime("%A").to_s %> </th> 
    <% end %> 
</thead> 
<tbody> 
    <tr> 
    <% @dates.each do |date| %> 
     <% if @time_entry %> 
     <td><%= text_field_tag "#{date}", @time_entry.hour_per_day["#{date}"], class: "dates" %></td> 
     <% else %> 
     <%if date < Date.today %> 
       <td> <%= text_field_tag "#{date}", "", class: "dates" %> </td> 
     <%else %> 
       <td><%= text_field_tag "#{date}", "", class: "dates" if date == Date.today && Time.now.strftime("%H").to_i >= 10 %> </td>   
     <% end %> 
    <% end %> 
    <% end %> 

    </tr> 


    <tr> 
    <% if @time_entry %> 
    <td colspan="2"> Please Enter your Comment </td> 

    <td colspan="5"> 
     <% @time_entry.comments.each do |c| %> 
      <p><%= text_field_tag "Comment", c.message %> </p> 
     <% end %> 
     </td> 

    <%else%> 
    <td colspan="2"> Please Enter your Comment </td> 

    <td colspan="5"><%= text_field_tag "Comment", "" %> 

    </td> 
    <%end%> 
    </tr> 

</tbody> 
</table> 
</div> 

    <button type="button" class="btn btn-primary" 
    id="save_entries">Submit</button> 
    <%= form_tag save_time_entries_path, method: 'post', 
    id:"save_time_entries" do %> 
    <%= hidden_field_tag "start_date", @dates.first%> 
    <%= hidden_field_tag "end_date", @dates.last%> 
    <%= hidden_field_tag "total_hours", "" %> 
    <%= hidden_field_tag "project_id", @project.id %> 
    <%= hidden_field_tag "time_entry", "" %> 

    <%= hidden_field_tag "message", "" %> 
    <% if @time_entry%> 
    <%= hidden_field_tag "time_entry_detail_id", @time_entry.id %> 

    <% end %> 
<% end %> 

    <script> 
    $("#save_entries").click(function(){ 
    var time_entry = [] 
    var hours = 0; 
    var message = document.getElementById("Comment").value; 
    $('.dates').each(function() { 
     hours += Number($(this).val()); 
     if ($(this).val() == 0) 
     { 
     time_entry.push($(this).attr('name'),0) 
     }else{ 
     time_entry.push($(this).attr('name'),$(this).val()) 
     } 

    }); 
    if (hours > 60) { 
    alert("Total Hours Should be equal to 60"); 
    return false; 
    } 
    else { 
    $("#message").val(message); 
    $("#time_entry").val(time_entry); 
    $("#total_hours").val(hours); 
    $("#save_time_entries").submit(); 
    } 
    }) 
    </script> 
<%end%> 

答えて

0

あなたがのform_forと2つの異なるフォームを作成することができ、それは私が把握問題

+0

ここでは、合計の時間を計算しています。だから私はform_tagを使ってこれを行う必要があります。フォームタグを使ってこれを行う方法については、 –

+0

https://apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag ですが、概念的には自分でフォームタグを書くようなものですが、それは誤って入力することを防ぐものです –

+0

この文書は既にチェックされています。それほど役に立たない。あなたのプロンプトのためにありがとう –

0

すべきではありません溶液を出します。同じページに2つの投稿フォームを使用することは悪い考えです。だから私は後のコールバックを作成し、TimeEntryDetailの既定値を与えて使用して、先週のレコードを作成しています。ここでは、すべての週レコードがバックグラウンドで作成されます。 6週間スキップした場合、バックグラウンドで空のレコードが6つ作成されます。ここに私の作業コードがあります

Modal.rb 

after_create :chceck_time 




    def check_time 


    @project_mem_check= ProjectMember.where(project_id: project, member_id: user).first.created_at 
    #present week dates coming here | 
    @time_entry_check = TimeEntryDetail.where(project_id: project, user_id: user).first.created_at 


    if @time_entry_check.nil? 
     @[email protected]_mem_check 
    end 

    @last_week_start=Date.today.beginning_of_week 
    @last_week_end =Date.today.end_of_week 

#cheking project member creation shall be less than time entry creation of that project. 
if @project_mem_check <= @time_entry_check 

    @time_check_first=TimeEntryDetail.where(project_id: project, user_id: user).pluck(:start_date).first.to_date 
    @time_check_last =TimeEntryDetail.where(project_id: project, user_id: user).pluck(:start_date).last.to_date 
    #check first entry == last time entry 
    if @[email protected]_check_last 

     pm=ProjectMember.where(project_id: project, member_id: user).first.created_at.to_date 
     tm=Date.today.to_date 

     unfilled_weeks= ((tm-pm)/7).to_i+1 



     unfilled_weeks.times { 

        @last_week_start= @last_week_start-7 
        @last_week_end = @last_week_end-7 



        @call_back_entry=TimeEntryDetail.create(start_date: (@last_week_start).to_date, end_date: (@last_week_end).to_date, 
        project_id: project.id, user_id: user.id, hours: 0, aasm_state: "pending", hour_per_day: "") 

        @call_back_entry.comments << Comment.create(message: "Please fill the timesheet ") 

     } 

    else 

     #Already this user have time entries and create empty records from where he stopped. 

     pm=created_at.to_date #--.present time entry is coming 
     tm=Date.today.to_date 

     unfilled_weeks= ((tm-pm)/7).to_i+1 



     unfilled_weeks.times{ 

        @last_week_start= @last_week_start-7 
        @last_week_end = @last_week_end-7 

        @call_back_entry=TimeEntryDetail.create(start_date: (@last_week_start).to_date, end_date: (@last_week_end).to_date, 
        project_id: project.id, user_id: user.id, hours: 0, aasm_state: "submitted", hour_per_day: "") 

        @call_back_entry.comments << Comment.create(message: "Not yet filled this week ") 


     } 

    end 

    end 
end 
関連する問題