为什么我的提交按钮不会做任何事情?

时间:2016-05-24 16:46:56

标签: ruby-on-rails

我在这里有一个表单,当我按下按钮时,我在控制台中没有输出。我在下面添加了整个表格。我不是这个表单的原作者,我只是想添加一个功能来编辑这个表单上的主题。

查看:

      <%= form_tag :action => 'update_subject', :controller => 'task', :id => @task.id do -%>
  <tr>

    <td><b>Subject:</b></td>
    <td><div id='edit_subject_div'><%=text_field_tag 'subject', @task.subject %></div></td>
  </tr>
   <tr>
     <td></td>
     <td><%=submit_tag "Submit", data: { disable_with: "Please wait..."}%></td>
   </tr>
   <%end -%>

动作:

  def update_subject
    @task = Task.find_by_id(params[:id])
    if @task.subject != params[:subject]
      @task.subject = params[:subject]
      @task.save!
    end
  end

整体表格:

    <script>
  $(document).ready(function(){
    $('select#activity_category_id').chained('select#cat_type_id');
    $('#activity_category_id').css('border', 'Silver 1px solid');
    $('#activity_location_id').css('border', 'Silver 1px solid');
    $('#activity_location_modifier_id').css('border', 'Silver 1px solid');
    //$('a[rel*=facebox]').facebox();
  });
  $(document).on('ajax:before', function(){
    $.facebox.loading();
  });

</script>
<h2>Task Detail:</h2><br/>
<%
   @task = Task.find(params[:task_id].to_i) if params[:task_id]
   @devices = [["",""]] + Device.where(job_id: @task.job_id, active: true).order(:name).pluck('name', 'id')
    @hours = (0..24).to_a
    @minutes = [0, 15, 30, 45]
    @today = Date.today
    @priorities = ["High", "Medium", "Low"]
   @user_list=[["",""]] + User.where(:active=>true).order('nickname').pluck('nickname', 'id')
   @groups=Group.order('name').pluck('name', 'id')
   @groups=@groups.each do |g| g[1]='group' + g[1].to_s end
   @users = @user_list + @groups
   @location=Location.where(active: true).order('description').pluck('description', 'id')
   @location_modifier=LocationModifier.where(active: true).order('description').pluck('description', 'id')
   #@categories = Category.where(active: 1).order(:description).pluck('description', 'id')
    params[:sort].nil? ? sort_str = "" : sort_str = params[:sort]
    params[:hist_length].nil? ? hist_str = "" : hist_str = params[:hist_length]
    @return_url = params[:return_url]
%>


<table style="border:none;width:90%">
<tr><td style="width:38%;vertical-align: top">
<table style="border:none;width:90%">
  <tr><td><b>Task ID:</b></td>
    <td><%=@task.id.to_s%></td></tr>
  <tr>
    <td><b>Method of Contact:</b></td>
    <td><%= @task.contact_type %></td>
  </tr>
  <tr>
    <td><b>Contacted by:</b></td>
    <td><%= @task.customer_contact %></td>
  </tr>
  <tr>
    <td><b>Task Created by:</b></td>
    <td><%= @task.created_by.nickname %></td>
  </tr>
<% if !@task.parent_task_id.nil? then %>
  <tr>
    <td><b>Task Split from:</b></td>
    <td><%= link_to 'Parent Task', :action => 'edit', :id => @task.parent_task_id %></td>
  </tr>
<% end %>
  <%= form_for @task do |f|%>
  <tr>

    <td><b><%= f.label :subject %>:</b></td>
    <td><div id='edit_subject_div'><%= f.text_field :subject %></div></td>
  </tr>
   <tr>
     <td></td>
     <td><%= f.submit %></td>
   </tr>
   <%end %>
  <tr>
    <td colspan=2><b>Description:</b></td>
  </tr>c

  <tr>
    <td colspan=2><%=simple_format(@task.description)%></td>
  </tr>
  <tr><td colspan=2>&nbsp;</td></tr>
  <tr>
    <td><b>Task Initiated:</b></td>
    <td><%= @task.created_at.strftime("%H:%M %p %m/%d/%y") %>&nbsp;&nbsp;<%= (Date.today - @task.problem_on).to_i %> Days Old</td>
  </tr>
  <tr>
    <td><b>Assigned Due Date:</b></td>
    <td><%= @task.due_on.strftime("%m/%d/%y")  %></td>
  </tr>
  <tr>
    <td><b>Status:</b></td>
    <td>
    <% if @task.resolved_on %>
    Resolved on <%= @task.resolved_on.strftime("%m/%d/%y") %>
    <% elsif @today < @task.due_on %>
    Due in <%= (@task.due_on - @today).to_i %> Days
    <% elsif @today > @task.due_on %>
    <%= (@today - @task.due_on).to_i %> Days Overdue
    <% else %>
    Due Today
    <% end %>
    </td>
  </tr>
 <tr>
    <td><b>Device:</b></td>
    <td><%= @task.device.nil? ? "None Assigned" : @task.device.name %></td>
  </tr>
  <tr>
    <td><b>Assigned To:</b></td>
    <% if !@task.assigned_to.nil? then %>
    <td><%=@task.assigned_to.nickname %></td>
    <% elsif !@task.group.nil? then %>
    <td><%=@task.group.name%></td>
    <% else %>
    <td>Unassigned</td>
    <% end %>
  </tr>
  <tr>
    <td><b>Priority:</b></td>
    <td><%=@task.priority %></td>
  </tr>
  <tr>
    <td><b>Resolved:</b></td>
    <% if !@task.resolved_on.nil? then %>
    <td><%=@task.resolved_on.strftime("%m/%d/%Y") %></td>
    <% else %>
    <td>Unresolved</td>
    <% end %>
  </tr>
</table>
<%=form_tag :action => 'update', :controller => 'task', :id => @task.id do -%><%=hidden_field_tag 'from_project_track', 'yes'%><%=hidden_field_tag 'return_path', @return_url%>
<table>
  <tr><td colspan=2><b>New Note:</b></td></tr>
  <tr>
    <td colspan=2><%= text_area_tag 'note', nil,  :size => '50x5' %></td>
  </tr>
   <% if !@task.device_id.nil? then %>
  <tr>
    <td><label for="task_device_id">Device</label>:</td>
    <td><%=select_tag 'task[device_id]', options_for_select(@devices, @task.device_id) %></td>
    <td>&nbsp;</td>
  </tr>
  <%else%>
  <tr>
    <td><label for="task_device_id">Device</label>:</td>
    <td><%=select_tag 'task[device_id]', options_for_select(@devices, '') %></td>
    <td>&nbsp;</td>
  </tr>
  <% end %>
  <tr>
    <td>Assigned Due Date:</td>
    <td><%= date_select 'task', 'due_on'  %></td>
     <td>&nbsp;</td>
  </tr>
    <% if !@task.assigned_to_id.nil? then %>
  <tr>
    <td><label for="task_assigned_to">Assigned To</label>:</td>
    <td><%= select_tag 'task[assigned_to_id]', options_for_select(@users, :selected=>@task.assigned_to_id.to_s) %></td>
    <td>&nbsp;</td>
  </tr>
    <% elsif !@task.group_id.nil? then %>
  <tr>
    <td><label for="task_assigned_to">Assigned To</label>:</td>
    <td><%= select_tag 'task[assigned_to_id]', options_for_select(@users, 'group' + @task.group_id.to_s) %></td>
    <td>&nbsp;</td>
  </tr>
    <% else %>
  <tr>
    <td><label for="task_assigned_to">Assigned To</label>:</td>
    <td><%= select_tag 'task[assigned_to_id]', options_for_select(@users) %></td>
    <td>&nbsp;</td>
  </tr>
    <% end %>
    <tr>
    <td><label for="task_priority">Priority:</label></td>
    <td><%= select 'task', 'priority', @priorities  %></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Resolved:</td>
    <td><%= check_box_tag 'resolved', 1, @task.resolved_on %></td>
  </tr>

  <tr>
    <td>Bill:</td>
    <td colspan="2"><%= select_tag 'bill_hours', options_for_select(@hours, 0)  %>:<%= select_tag 'bill_minutes', options_for_select(@minutes, 0)  %></td>
  </tr>
  <tr>
    <td>Nonbill:</td>
    <td colspan="2"><%= select_tag 'nonbill_hours', options_for_select(@hours, 0)  %>:<%= select_tag 'nonbill_minutes', options_for_select(@minutes, 0)  %></td>
  </tr>
  <tr>
    <td>Category:</td>
    <td colspan="2">
      <%=select_tag 'cat_type_id',options_for_select(@cat_type_list,@selected_cat_type), {:prompt=>"Select Category Type"}%><br/>
      <select id="activity_category_id" prompt="Select Category" name="activity[category_id]" style="width:300px;">
        <option value=''>Select Category</option>
        <%@cat_list.each do |c|%>
            <option value="<%=c.id%>" <%=(!@activity.nil? && @activity.category_id==c.id) ? "selected" : "" %> class="<%=c.cat_type_id.nil? ? '':c.cat_type_id%>"><%=c.short_description.nil? ? c.description : c.short_description%></option>
        <%end%>
      </select></td>
  </tr>
  <tr>
    <td>Location:</td>
    <td><%= select 'activity', 'location_id',[["",""]] + @location %></td>
  </tr>
  <tr>
    <td>Location Modifier:</td>
    <td><%= select 'activity', 'location_modifier_id',[["",""]] + @location_modifier %></td>
  </tr>
  <tr>
    <td>Review?:</td>
    <td colspan="2"><%= check_box 'activity', 'review' %></td>
  </tr>


  <tr>
    <td colspan=2 align="CENTER"><%=submit_tag "Submit", id: 'commit', data: { disable_with: "Please wait..."}%></td>
  </tr>
</table>
<% end -%>

</td>
<% if params[:mobile] == "1" then %>
</tr>
<tr>
<% end %>
<td valign="TOP">
<table style="width:90%;padding:0;">
<tr><td colspan="2" ><%=link_to 'Task Time Report', :controller=>'time', :action=>'list', :task_id=>@task.id %></td></tr>
<tr><td colspan="2" align="LEFT"><b>Notes:</b></td></tr>
<% @notes = Note.where(task_id: @task.id).order('created_at') %>
<% @notes.each do |n| %>
<tr style="background-color: <%=cycle("white","silver")%>;"><td valign="TOP"><b><%=n.created_by.nil? ? "Error" : n.created_by.nickname %><br>(<%=n.created_at.strftime("%m/%d/%Y %I:%M")%>)</b></td><td valign="TOP"><%=simple_format(n.content)%></td></tr>
<% end %>
</table>


</td></tr>
</table>

3 个答案:

答案 0 :(得分:0)

尝试使用form_for,因为您要修改现有对象@task

<%= form_for :task, @task, :url => { :action => :update_subject} do |f| %>
  <%= f.label :subject %>:
  <%= f.text_field :subject %>
  <%= f.submit %>
<% end %>

答案 1 :(得分:0)

从Javascript中删除以下行:

$('form').submit(confirmSelections);

这将不再执行动态表单验证,但应允许Submit按钮起作用。真正的问题将出现在confirmSelections逻辑中的某个位置,这将取决于在表单上输入的数据。

作为旁注,这段代码看起来需要一些严肃的返工。至少:

  • 使用form_for代替form_tag
  • 使用f.submit代替submit_tag(一旦实现了form_for)
  • 所有Javascript都是静态的,应该移动到资产管道中,而不是包含在内的
  • 紧跟在<h2>Task Detail:</h2>之后的块中的所有Ruby代码都应该移动到控制器/操作
  • @notes = Note.where(task_id: @task.id).order('created_at')的代码应移至控制器/操作
  • Javascript应该选择CSS类,而不是显式的CSS样式,以便您的演示文稿能够轻松维护

这些实际上应该有助于使这个模板合理地维护并将MVC问题彼此分开。

答案 2 :(得分:0)

不确定为什么或如何修复它但我删除了第一个表单并使第二个表单覆盖了整个视图。这方面的缺点是我必须提交整个页面才能更新主题。

<script>
  $(document).ready(function(){
    $('select#activity_category_id').chained('select#cat_type_id');
    $('#activity_category_id').css('border', 'Silver 1px solid');
    $('#activity_location_id').css('border', 'Silver 1px solid');
    $('#activity_location_modifier_id').css('border', 'Silver 1px solid');
    //$('a[rel*=facebox]').facebox();
  });
  $(document).on('ajax:before', function(){
    $.facebox.loading();
  });
  function confirmSelections(){
    valid=false;
    valid1=false;
    valid2=false;
    if ($('#activity_category_id').val()==""){
      $('#activity_category_id').css('border', 'red 2px solid');
      valid= false;
    }else{
      $('#activity_category_id').css('border', 'Silver 1px solid');
      valid= true;
    }
    if ($('#activity_location_id').val()=="")
    {
      $('#activity_location_id').css('border', 'red 2px solid');
      valid1=false;
    }
    else
    {$('#activity_location_id').css('border', 'Silver 1px solid');
      valid1=true;}
    if ($('#activity_location_modifier_id').val()=="")
    {$('#activity_location_modifier_id').css('border', 'red 2px solid');
      valid2=false;
    }else{
      $('#activity_location_modifier_id').css('border', 'Silver 1px solid');
      valid2= true;
    }
    if (valid && valid1 && valid2)
    {return true;}
    else
    {
      if (($('#nonbill_hours').val()!='0' || $('#bill_hours').val()!='0' || $('#nonbill_minutes').val()!='0' || $('#bill_minutes').val()!='0' || $('#nonbill_hours').val()!='0') && ($('#activity_category_id').val()=='')){
        var answer = (confirm('No category is selected. Click Cancel to select. Click OK to update the task with no time entry.')) ;
        return answer;
      }
      if (($('#nonbill_hours').val()!='0' || $('#bill_hours').val()!='0' || $('#nonbill_minutes').val()!='0' || $('#bill_minutes').val()!='0' || $('#nonbill_hours').val()!='0') && ($('#activity_location_id').val()=='')){
        var answer = (confirm('No location is selected. Click Cancel to select. Click OK to update the task with no time entry.')) ;
        return answer;
      }
      if (($('#nonbill_hours').val()!='0' || $('#bill_hours').val()!='0' || $('#nonbill_minutes').val()!='0' || $('#bill_minutes').val()!='0' || $('#nonbill_hours').val()!='0') && ($('#activity_location_modifier_id').val()=='')){
        var answer = (confirm('No location modifier is selected. Click Cancel to select. Click OK to update the task with no time entry.')) ;
        return answer;
      }
    }
  }
  $('form').submit(confirmSelections);
</script>
<h2>Task Detail:</h2><br/>
<%
   @task = Task.find(params[:task_id].to_i) if params[:task_id]
   @devices = [["",""]] + Device.where(job_id: @task.job_id, active: true).order(:name).pluck('name', 'id')
    @hours = (0..24).to_a
    @minutes = [0, 15, 30, 45]
    @today = Date.today
    @priorities = ["High", "Medium", "Low"]
   @user_list=[["",""]] + User.where(:active=>true).order('nickname').pluck('nickname', 'id')
   @groups=Group.order('name').pluck('name', 'id')
   @groups=@groups.each do |g| g[1]='group' + g[1].to_s end
   @users = @user_list + @groups
   @location=Location.where(active: true).order('description').pluck('description', 'id')
   @location_modifier=LocationModifier.where(active: true).order('description').pluck('description', 'id')
   #@categories = Category.where(active: 1).order(:description).pluck('description', 'id')
    params[:sort].nil? ? sort_str = "" : sort_str = params[:sort]
    params[:hist_length].nil? ? hist_str = "" : hist_str = params[:hist_length]
    @return_url = params[:return_url]
%>


<table style="border:none;width:90%">
<tr><td style="width:38%;vertical-align: top">
  <%=form_tag :action => 'update', :controller => 'task', :id => @task.id do -%><%=hidden_field_tag 'from_project_track', 'yes'%><%=hidden_field_tag 'return_path', @return_url%>
<table style="border:none;width:90%">
  <tr><td><b>Task ID:</b></td>
    <td><%=@task.id.to_s%></td></tr>
  <tr>
    <td><b>Method of Contact:</b></td>
    <td><%= @task.contact_type %></td>
  </tr>
  <tr>
    <td><b>Contacted by:</b></td>
    <td><%= @task.customer_contact %></td>
  </tr>
  <tr>
    <td><b>Task Created by:</b></td>
    <td><%= @task.created_by.nickname %></td>
  </tr>
<% if !@task.parent_task_id.nil? then %>
  <tr>
    <td><b>Task Split from:</b></td>
    <td><%= link_to 'Parent Task', :action => 'edit', :id => @task.parent_task_id %></td>
  </tr>
<% end %>
  <tr>
    <td><b>Subject:</b></td>
    <td><div id='edit_subject_div'><%= text_field_tag 'task[subject]', @task.subject%></div></td>
  </tr>
  <tr>
    <td colspan=2><b>Description:</b></td>
  </tr>
  <tr>
    <td colspan=2><%=simple_format(@task.description)%></td>
  </tr>
  <tr><td colspan=2>&nbsp;</td></tr>
  <tr>
    <td><b>Task Initiated:</b></td>
    <td><%= @task.created_at.strftime("%H:%M %p %m/%d/%y") %>&nbsp;&nbsp;<%= (Date.today - @task.problem_on).to_i %> Days Old</td>
  </tr>
  <tr>
    <td><b>Assigned Due Date:</b></td>
    <td><%= @task.due_on.strftime("%m/%d/%y")  %></td>
  </tr>
  <tr>
    <td><b>Status:</b></td>
    <td>
    <% if @task.resolved_on %>
    Resolved on <%= @task.resolved_on.strftime("%m/%d/%y") %>
    <% elsif @today < @task.due_on %>
    Due in <%= (@task.due_on - @today).to_i %> Days
    <% elsif @today > @task.due_on %>
    <%= (@today - @task.due_on).to_i %> Days Overdue
    <% else %>
    Due Today
    <% end %>
    </td>
  </tr>
 <tr>
    <td><b>Device:</b></td>
    <td><%= @task.device.nil? ? "None Assigned" : @task.device.name %></td>
  </tr>
  <tr>
    <td><b>Assigned To:</b></td>
    <% if !@task.assigned_to.nil? then %>
    <td><%=@task.assigned_to.nickname %></td>
    <% elsif !@task.group.nil? then %>
    <td><%=@task.group.name%></td>
    <% else %>
    <td>Unassigned</td>
    <% end %>
  </tr>
  <tr>
    <td><b>Priority:</b></td>
    <td><%=@task.priority %></td>
  </tr>
  <tr>
    <td><b>Resolved:</b></td>
    <% if !@task.resolved_on.nil? then %>
    <td><%=@task.resolved_on.strftime("%m/%d/%Y") %></td>
    <% else %>
    <td>Unresolved</td>
    <% end %>
  </tr>
</table>

<table>
  <tr><td colspan=2><b>New Note:</b></td></tr>
  <tr>
    <td colspan=2><%= text_area_tag 'note', nil,  :size => '50x5' %></td>
  </tr>
   <% if !@task.device_id.nil? then %>
  <tr>
    <td><label for="task_device_id">Device</label>:</td>
    <td><%=select_tag 'task[device_id]', options_for_select(@devices, @task.device_id) %></td>
    <td>&nbsp;</td>
  </tr>
  <%else%>
  <tr>
    <td><label for="task_device_id">Device</label>:</td>
    <td><%=select_tag 'task[device_id]', options_for_select(@devices, '') %></td>
    <td>&nbsp;</td>
  </tr>
  <% end %>
  <tr>
    <td>Assigned Due Date:</td>
    <td><%= date_select 'task', 'due_on'  %></td>
     <td>&nbsp;</td>
  </tr>
    <% if !@task.assigned_to_id.nil? then %>
  <tr>
    <td><label for="task_assigned_to">Assigned To</label>:</td>
    <td><%= select_tag 'task[assigned_to_id]', options_for_select(@users, :selected=>@task.assigned_to_id.to_s) %></td>
    <td>&nbsp;</td>
  </tr>
    <% elsif !@task.group_id.nil? then %>
  <tr>
    <td><label for="task_assigned_to">Assigned To</label>:</td>
    <td><%= select_tag 'task[assigned_to_id]', options_for_select(@users, 'group' + @task.group_id.to_s) %></td>
    <td>&nbsp;</td>
  </tr>
    <% else %>
  <tr>
    <td><label for="task_assigned_to">Assigned To</label>:</td>
    <td><%= select_tag 'task[assigned_to_id]', options_for_select(@users) %></td>
    <td>&nbsp;</td>
  </tr>
    <% end %>
    <tr>
    <td><label for="task_priority">Priority:</label></td>
    <td><%= select 'task', 'priority', @priorities  %></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Resolved:</td>
    <td><%= check_box_tag 'resolved', 1, @task.resolved_on %></td>
  </tr>

  <tr>
    <td>Bill:</td>
    <td colspan="2"><%= select_tag 'bill_hours', options_for_select(@hours, 0)  %>:<%= select_tag 'bill_minutes', options_for_select(@minutes, 0)  %></td>
  </tr>
  <tr>
    <td>Nonbill:</td>
    <td colspan="2"><%= select_tag 'nonbill_hours', options_for_select(@hours, 0)  %>:<%= select_tag 'nonbill_minutes', options_for_select(@minutes, 0)  %></td>
  </tr>
  <tr>
    <td>Category:</td>
    <td colspan="2">
      <%=select_tag 'cat_type_id',options_for_select(@cat_type_list,@selected_cat_type), {:prompt=>"Select Category Type"}%><br/>
      <select id="activity_category_id" prompt="Select Category" name="activity[category_id]" style="width:300px;">
        <option value=''>Select Category</option>
        <%@cat_list.each do |c|%>
            <option value="<%=c.id%>" <%=(!@activity.nil? && @activity.category_id==c.id) ? "selected" : "" %> class="<%=c.cat_type_id.nil? ? '':c.cat_type_id%>"><%=c.short_description.nil? ? c.description : c.short_description%></option>
        <%end%>
      </select></td>
  </tr>
  <tr>
    <td>Location:</td>
    <td><%= select 'activity', 'location_id',[["",""]] + @location %></td>
  </tr>
  <tr>
    <td>Location Modifier:</td>
    <td><%= select 'activity', 'location_modifier_id',[["",""]] + @location_modifier %></td>
  </tr>
  <tr>
    <td>Review?:</td>
    <td colspan="2"><%= check_box 'activity', 'review' %></td>
  </tr>


  <tr>
    <td colspan=2 align="CENTER"><%=submit_tag "Submit", id: 'commit', data: { disable_with: "Please wait..."}%></td>
  </tr>
</table>
<% end -%>

</td>
<% if params[:mobile] == "1" then %>
</tr>
<tr>
<% end %>
<td valign="TOP">
<table style="width:90%;padding:0;">
<tr><td colspan="2" ><%=link_to 'Task Time Report', :controller=>'time', :action=>'list', :task_id=>@task.id %></td></tr>
<tr><td colspan="2" align="LEFT"><b>Notes:</b></td></tr>
<% @notes = Note.where(task_id: @task.id).order('created_at') %>
<% @notes.each do |n| %>
<tr style="background-color: <%=cycle("white","silver")%>;"><td valign="TOP"><b><%=n.created_by.nil? ? "Error" : n.created_by.nickname %><br>(<%=n.created_at.strftime("%m/%d/%Y %I:%M")%>)</b></td><td valign="TOP"><%=simple_format(n.content)%></td></tr>
<% end %>
</table>


</td></tr>
</table>