嗨朋友,我帶來了一個新問題。在我的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" /> 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)
這裏我用擔憂的壓延display.But原因Ĵ查詢日期選擇器,爲什麼我的任務沒有保存?請指導我。提前致謝。
第一步:請顯示錯誤頁面的POST數據。 – jimworm
另外:你的控制檯在'index'動作中顯示'GET',但你正在談論'create'? – jimworm
您好吉姆我呈現我的_form.hml.erb空間索引頁,因此這種混淆 – Ravindra