我有一個項目模型。我最近將project_name添加到該模型中。當我在該字段中輸入名稱並創建新項目時,它不會保存新屬性。Rails不顯示保存使用update_attributes的模型屬性
項目控制器:
def create
if project.update_attributes(project_params)
flash[:success] = 'Project was successfully created'
redirect_to project_url(project)
else
render :new
end
end
def project_params
params.require(:project).permit(:description, :project_name, :customer_number, :sales_number, :sales_id, :customer_id, :discount, :date_wanted, :price,
items_attributes: [:id, :name, :unit, :quantity, :price, :_destroy])
end
項目型號:
include AASM
STEP_FORWARD_STATE = {
'quotes' => :proposal,
'proposals' => :ordered,
'orders' => :active,
'in_process' => :close
}
STEP_BACK_STATE = {
'proposals' => :quoted,
'orders' => :proposal,
'in_process' => :ordered,
'closed' => :active
}
belongs_to :customer
belongs_to :sales, class_name: 'User'
has_many :curtains, dependent: :destroy
has_many :versions, class_name: 'ProjectVersion', dependent: :destroy
has_many :tasks, dependent: :destroy
has_many :drape_tasks, class_name: 'DrapeTask', dependent: :destroy
has_many :trough_tasks, class_name: 'TroughTask', dependent: :destroy
has_many :items, dependent: :destroy
accepts_nested_attributes_for :items, :allow_destroy => true
delegate :email, :first_name, :full_name, :city_address, :name_for_select,
:contry_address, :address_for_email, :phone_o,
to: :customer, prefix: true, :allow_nil => true
delegate :name_for_select, :address_for_email, :email, :city_address, :full_name, :rep_number, :first_title,
to: :sales, prefix: true, :allow_nil => true
validates :customer, :sales, presence: true
attr_accessor :update_event
終端登錄時動作:
Started POST "/projects" for ::1 at 2016-06-08 15:54:57 -0400
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+vpCG6JPLLSBtntKhudPTopzq/MH0YlnjlfnFSsOVYnMfRY888LafcReDF41GIxb14q6DLdkbz/AwXEQi+iF4w==", "commit"=>"Create Project", "name"=>"TEST DAMMIT", "customer_autocomplete"=>"[email protected]", "project"=>{"customer_id"=>"1", "sales_id"=>"1", "discount"=>""}, "sales_autocomplete"=>"[email protected]", "description_autocomplete"=>"DID IT WORK"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Profile Load (1.8ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."person_id" = $1 AND "profiles"."person_type" = $2 LIMIT 1 [["person_id", 1], ["person_type", "User"]]
Project Load (1.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" IS NULL ORDER BY "projects"."id" ASC LIMIT 1
(0.2ms) BEGIN
Customer Load (0.5ms) SELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT 1 [["id", 1]]
Profile Load (1.2ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."person_id" = $1 AND "profiles"."person_type" = $2 LIMIT 1 [["person_id", 1], ["person_type", "Customer"]]
User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."person_id" = $1 AND "profiles"."person_type" = $2 LIMIT 1 [["person_id", 1], ["person_type", "User"]]
SQL (0.8ms) INSERT INTO "projects" ("state", "sales_id", "customer_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["state", "quotes"], ["sales_id", 1], ["customer_id", 1], ["created_at", "2016-06-08 19:54:57.746357"], ["updated_at", "2016-06-08 19:54:57.746357"]]
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."project_id" = $1 [["project_id", 13]]
Curtain Load (0.2ms) SELECT "curtains".* FROM "curtains" WHERE "curtains"."project_id" = $1 [["project_id", 13]]
(1.2ms) COMMIT
Redirected to http://localhost:3000/projects/13
EDIT(6月9日):
感謝幫助如此遠,@ taryn-east。 (還添加上述澄清我project_params)
下面是我用PROJECT_NAME形式:
.form-group
= label_tag 'name', 'Project Name', class: 'control-label'
.controls
= text_field_tag 'name', project.project_name, class: 'form-control', autocomplete: :off
爲了便於比較,這裏有用於模型的工作參數的一種形式:
.form-group
= label_tag 'customer_autocomplete', 'Customer', class: 'control-label'
.controls
= text_field_tag 'customer_autocomplete', project.customer_name_for_select, class: 'form-control customer-autocomplete', autocomplete: :off
= f.hidden_field :customer_id
顯然,缺少f.hidden_field:[parameter]。我把它添加到我的PROJECT_NAME場,離開它看起來像這樣:
.form-group
= label_tag 'name', 'Project Name', class: 'control-label'
.controls
= text_field_tag 'name', project.project_name, class: 'form-control', autocomplete: :off
= f.hidden_field :project_name
現在留給我們的終端日誌:
Started POST "/projects" for ::1 at 2016-06-09 12:11:42 -0400
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BqEZsyQK+uLoeWewYC6smeC6CX0EGLcZgUQlmAyjtLqOmAb3v+2pw6VJ0hoBnISkGegz3fCAMT4+UPMVh28l8Q==", "commit"=>"Create Project", "project_name"=>"TEST PLS DEALER", "project"=>{"project_name"=>"", "customer_id"=>"1", "sales_id"=>"1", "discount"=>"", "description"=>""}, "customer_autocomplete"=>"[email protected]", "sales_autocomplete"=>"[email protected]", "description_autocomplete"=>"WORK PLS ONE TIME EHHHH"}
這是有前途的,如添加
f.hidden_field :project_name
將project_name添加到創建操作中傳遞的項目哈希中,如下所示:
"project"=>{"project_name"=>"", "customer_id"=>"1", "sales_id"=>"1", "discount"=>"", "description"=>""}
但是,您可以看到參數的實際內容仍然位於應該駐留的字段之外。我做了一些挖掘,並注意到我的字段和以前的工作字段之間的區別:工作的字段有類:「 [:parameter] -autocomplete「。我添加了適當的課程,但仍然無效。在亞洲其他應用程序,有一個CoffeeScript的:
$(document).on 'focus', '.customer-autocomplete', ->
$(@).autocomplete
source: "/customers",
dataType: 'JSON',
minLength: 2,
select: (event, ui) ->
$('#project_customer_id').val ui.item.customer_id
$('#sales_autocomplete').val ui.item.sales_data
$('#project_sales_id').val ui.item.sales_id
$(document).on 'focus', '.sales-autocomplete', ->
$(@).autocomplete
source: "/sales",
dataType: 'JSON',
minLength: 2,
select: (event, ui) ->
$('#project_sales_id').val ui.item.sales_id
我不熟悉的CoffeeScript,但看起來這是何等的自動完成與類的隱藏字段:「[:參數] -autocomplete」。我如何複製這個我添加的領域?
編輯:(6月10日)
我把你的建議,簡化了遷移(絕對是一個良好的通話:)),但它似乎仍然是不合作。 新PARAMS:
def project_params
params.require(:project).permit(:description, :name, :customer_number, :sales_number, :sales_id, :customer_id, :discount, :date_wanted, :price,
items_attributes: [:id, :name, :unit, :quantity, :price, :_destroy])
end
新形式:
.form-group
= label_tag 'name', 'Project Name', class: 'control-label'
.controls
= text_field_tag 'name', project.name, class: 'form-control name-autocomplete', autocomplete: :off
= f.hidden_field :name
新顯示:
Started POST "/projects" for ::1 at 2016-06-10 10:30:30 -0400
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iMt4rowEbO1UGzjwfl1XkraTbktC+5+/MiNzYvU/uxAA8mfqF+M/zBkrjVof73+vT8FU67ZjGZiNN6XvfvMqWw==", "commit"=>"Create Project", "name"=>"TEST PROJECT NAME", "project"=>{"name"=>"", "customer_id"=>"1", "sales_id"=>"1", "discount"=>"", "description"=>""}, "customer_autocomplete"=>"[email protected]", "sales_autocomplete"=>"[email protected]", "description_autocomplete"=>"TEST PROJECT DESCRIPTION"}
:
Project Name:
= project.name
終端從企圖使上述項目日誌我有這個你也可以看到ame問題與project.description一樣;是否有可能在應用程序中的其他地方定義了「項目」變量,但不包含這些新參數?在這個過程中的任何一點都不會拋出紅色的屏幕。再次感謝所有幫助迄今爲止@taryn :)
是什麼在你的控制器的'project_params'中,你可以發佈該方法嗎?你可以發表你的表格嗎? – chrismanderson
更新後嘗試'project.reload'或使用'update_attributes!'更新項目。 –