我有一个项目模型。我最近将project_name添加到该模型中。当我在字段中输入名称并创建新项目时,它不会保存新属性。
项目管理员:
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"=>"test@test.com", "project"=>{"customer_id"=>"1", "sales_id"=>"1", "discount"=>""}, "sales_autocomplete"=>"dzk@dzk", "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
编辑(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:[参数]丢失了。我将它添加到我的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"=>"test@test.com", "sales_autocomplete"=>"dzk@dzk", "description_autocomplete"=>"WORK PLS ONE TIME EHHHH"}
这很有希望,因为添加
f.hidden_field :project_name
将project_name添加到Create操作中传递的Project哈希中,如下所示:
"project"=>{"project_name"=>"", "customer_id"=>"1", "sales_id"=>"1", "discount"=>"", "description"=>""}
然而,你可以看到参数的实际内容仍然在它应该驻留的字段之外。我做了一些挖掘并注意到我的字段和之前的工作字段之间的区别:工作的那些有类:“ [:参数] - 自动完成“在其中。我添加了适当的类,但它仍然没有用。在应用程序的其他地方,有一个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,但似乎这是用类自动完成隐藏字段:“[:parameter] -autocomplete”。如何为我添加的字段复制此内容?
编辑:( 6月10日)
我接受了你的建议并简化了迁移(绝对是一个很好的电话:))但它似乎仍然不合作。 新的参数:
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
新节目:
Project Name:
= project.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"=>"test@test.com", "sales_autocomplete"=>"dzk@dzk", "description_autocomplete"=>"TEST PROJECT DESCRIPTION"}
我也遇到了与project.description相同的问题,正如你所看到的那样; “项目”变量是否可能在应用程序中的其他位置定义,不包括这些新的参数?它也不会在过程中的任何一点抛出红色屏幕。再次感谢@taryn的所有帮助:)
答案 0 :(得分:0)
如果查看服务器窗口中的输出,它会显示问题:
"name"=>"TEST DAMMIT", "customer_autocomplete"=>"test@test.com",
"project"=>{"customer_id"=>"1", "sales_id"=>"1", "discount"=>""}
该名称字段不属于project
,因此不会被视为project
的属性。
你期望从params看到的是:
"customer_autocomplete"=>"test@test.com",
"project"=>{"name"=>"TEST DAMMIT", "customer_id"=>"1", "sales_id"=>"1", "discount"=>""}
这意味着您的表单出现了一些问题。检查您的name
字段是否与其他项目字段完成相同
编辑:
在表单中,属性名为name
:
text_field_tag 'name',
但是在代码的其余部分中,它期望该属性被命名为project_name
params.require(:project).permit(:description, :project_name
你必须实际命名该字段与你在代码中使用的字段相同:)例如
text_field_tag 'project_name',
虽然TBH我认真地建议只是调用它name
(并重写迁移等),如果可以的话因为project.project_name
有点多余;)