2016-06-09 88 views
0

我有一個項目模型。我最近將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 :)

+1

是什麼在你的控制器的'project_params'中,你可以發佈該方法嗎?你可以發表你的表格嗎? – chrismanderson

+0

更新後嘗試'project.reload'或使用'update_attributes!'更新項目。 –

回答

0

如果你看一下在服務器窗口輸出就說明你的問題:

"name"=>"TEST DAMMIT", "customer_autocomplete"=>"[email protected]", 
"project"=>{"customer_id"=>"1", "sales_id"=>"1", "discount"=>""} 

這個名字字段不是project的一部分,因此不會被拾取爲project的屬性。

,你希望看到PARAMS未來通過將這個什麼:

"customer_autocomplete"=>"[email protected]", 
"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 

你有實際名稱的字段一樣WHA T優使用詮釋他的代碼:)如

text_field_tag 'project_name', 

雖然TBH我會認真地建議只是調用它name(和重寫遷移等),如果可以的話,因爲project.project_name是有點多餘;)

+0

謝謝,@ taryn,這絕對有幫助。我添加了一些新的信息,你覺得怎麼樣? –

+0

好的,更新了我的答案:) –

+0

試過了,但那似乎沒有奏效。增加了一些更多的信息,看看是否有助於解決問題。 –