0
我有一個類Ticket,如下所示。驗證工作,但消息說「Customer can't be blank
」和「Builder can't be blank
。另外,在創建時,url表示localhost/tickets
。不應該是localhost/tickets/new
?:customer
和:builder
用於從現有的構建器列表中進行選擇,或者輸入新的建設者,它的工作原理。我也取在誤差塊的Customer
是清單,因爲我想不出還有什麼地方把它。我的驗證正在工作,但消息不是
class Ticket < ActiveRecord::Base
belongs_to :installer, class_name: "Customer", foreign_key: :builder_id
validates :customer, presence: { message: "test" }
validates :builder, presence: { message: "test" }
end
和控制器。
class TicketsController < ApplicationController
# GET /tickets/new
def new
@ticket = Ticket.new
@builders = Customer.all
end
before_action :set_ticket, only: [:show, :edit, :update, :destroy, :remove]
def create
@ticket = Ticket.new(ticket_params)
# New Builder
if builder = params[:ticket][:builder]
@ticket.customer = builder
c = Customer.create(name: builder, color: params[:ticket][:color])
@ticket.builder_id = c.id
elsif builder = params[:ticket][:customer] # Existing "Customer"
@ticket.builder_id = Customer.find_by_name(builder).id
end
if not params[:ticket][:color] # Existing Customer
color = Customer.find_by_name(builder).color
@ticket.color = color
end
calendar_date = Date.new(params[:ticket]["calendar_date(1i)"].to_i,
params[:ticket]["calendar_date(2i)"].to_i,
params[:ticket]["calendar_date(3i)"].to_i)
@ticket.calendar_order = Ticket.where(calendar_date: calendar_date).size
respond_to do |format|
if @ticket.save
format.html { redirect_to @ticket, notice: 'Job was successfully created.' }
format.json { render :show, status: :created, location: @ticket }
else
@builders = Customer.all
format.html { render :new }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end
# ..
private
# Use callbacks to share common setup or constraints between actions.
def set_ticket
@ticket = Ticket.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ticket_params
params.require(:ticket).permit(:customer,
:builder,
...)
end
end
奇怪的是,我原來的代碼開始工作,而這段代碼不會。 – 2015-03-25 09:04:54