控制台显示OK,但浏览器显示错误?

时间:2011-11-19 06:55:06

标签: ruby-on-rails validation

朋友们,我遇到了一个新问题。在我的rails应用程序中我有一个任务控制器和模型。当我尝试创建一个新任务时,我收到错误消息“due_date”不应该是空白,但当我检查我的控制台时有一个成功消息。这是我的代码:

我的控制器:

class TasksController < ApplicationController

  layout "payroll" , :except => [:closed,:today,:this_week, :this_month]

  # GET /tasks
  # GET /tasks.xml
  def index
    @menu = "Timesheet"
    @page_name = "Manage Task"
    company_id = Company.find_by_subdomain(request.subdomain)
    @users = User.find_all_by_company_id(company_id)
    @tasks = Task.find_all_by_assigned_to(session[:current_user_id].to_i)#.page(params[:page])
    @task = Task.new

    respond_to do |format|
      format.html # index.html.erb
      format.html # new.html.erb
      format.xml  { render :xml => @tasks }
    end
  end
 # POST /tasks
  # POST /tasks.xml
  def create
    @task = Task.new(params[:task])
    #@task.assigned_to = session[:user_id]
    @task.task_status = "0"

    respond_to do |format|
      if @task.save
         Email.task_created(@task).deliver
         @tasks = Task.all
         @task = Task.new
         format.html { redirect_to tasks_path }
         format.xml  { render :xml => @task, :status => :created, :location => @task }
      else
         format.html { render :action => "new" }
         format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end

我的模特:

class Task < ActiveRecord::Base
  #validation start 
  validates :description, :due_date, :assigned_to, :priority, :presence => true
  validates_length_of :description, :maximum =>500
 end 

和查看:

<%= form_for(@task) do |f| %>
    <%= render 'shared/form_error', :object => @task %>

    <div class="field">
      <%= f.label :description %><br />
      <%= f.text_area :description ,:rows => "2", :cols => "20" %>
    </div>
    <div class="field">
      <%= f.label :due_date %><br />
      <%= f.text_field :due_date %>
    </div>
    <div class="field">
      <%= f.label :assigned_to %><br />
      <%= collection_select(:task, :assigned_to, User.all, :id, :first_name, :prompt =>true ) %>
    </div>
    <div class="field">
      <%= f.label :priority %><br />
      <%= f.select (:priority, [['High'],['Medium'],['Low']]) %>
    </div>

    <div class="actions">
      <button type="submit" class="btn"><img src="/images/add.png" />&nbsp; Add This Task</button>
    </div>
<% end %

我的控制台消息:

Started GET "/tasks" for 127.0.0.1 at Sat Nov 19 12:27:10 +0530 2011
  Processing by TasksController#index as HTML
  ←[1m←[36mCompany Load (0.0ms)←[0m  ←[1mSELECT "companies".* FROM "companies" WHERE "companies"."subdomain" = 'rsgroup'
 LIMIT 1←[0m
  ←[1m←[35mUser Load (0.0ms)←[0m  SELECT "users".* FROM "users" WHERE "users"."company_id" = 1
  ←[1m←[36mTask Load (0.0ms)←[0m  ←[1mSELECT "tasks".* FROM "tasks" WHERE "tasks"."assigned_to" = 1←[0m
Rendered shared/_page_title.html.erb (0.0ms)
Rendered shared/_no_record.html.erb (0.0ms)
Rendered tasks/_task_table.html.erb (5.0ms)
Rendered shared/_form_error.html.erb (1.0ms)
  ←[1m←[35mUser Load (0.0ms)←[0m  SELECT "users".* FROM "users"
Rendered tasks/_form.html.erb (130.0ms)
Rendered shared/_sidebar.html.erb (0.0ms)
Rendered shared/_payroll_footer.html.erb (1.0ms)
Rendered tasks/index.html.erb within layouts/payroll (173.0ms)
Completed 200 OK in 253ms (Views: 183.0ms | ActiveRecord: 0.0ms)

这里我使用j查询日期选择器进行日历显示。但令人担忧的是为什么我的任务没有保存?请指导我。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您的代码中有错误

  

format.xml { render :xml => @task, :status => :created, :location => @task }

保存后和@task = Task.new分配后调用

同样错误可能是POSTed的due_date格式无效。正如@jimworm所请求的那样,提供POST请求,而不是索引页面请求。根据该请求,我们可以提交参数,您可以尝试rails c从提交的字符串中创建Date并检查它是否为nil

答案 1 :(得分:0)

我的朋友们,我得到了一个答案,我刚刚在我的公共文件夹中添加了几个css和java脚本文件,问题就解决了。