2015-11-06 32 views
-3

我更新方法有以下問題。Update_method不起作用

這裏是我的代碼:

def edit 
    @work_package = WorkPackage.find(params[:package]) 
    unless current_user.admin? || own_project?(@project) 
    flash[:danger] = I18n.t('messages.errors.no_admin_rights_for_site') 
    redirect_to projects_path 
    return 
end 
end 

def update 
    @work_package = WorkPackage.find(params[:id]) 
    if @work_package.update_attributes(filter_update_work_package_params) 
    flash[:success] = updated_success_message(I18n.t('objects.work_package')) 
    redirect_to project_path(@work_package.project_id) 
else 
    @object = @work_package 
    render 'edit' 
end 
end 

這我work_package PARAMS

private 

def filter_work_package_params 
    params.require(:work_package).permit(:name, :parent_id, :project_id, :started_on, :finished_on) 
end 

def filter_update_work_package_params 
    params.permit(:package_id, :project_id, :team_id, :person_month, :started_on, :finished_on) 
end 

當我點擊提交,什麼都沒有發生

這裏是控制檯日誌:

Started PATCH "/work_packages/33" for ::1 at 2015-11-06 09:14:49 +0100 
Processing by WorkPackagesController#update as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"R6k9B20nfUj4BSRDcA9+D+V73ihR9l9AzjFtBIoDYFGhxCkO+WsqOQzWhG7NCprxcQ3juJahH46ftHB94b3YNg==", "work_package"=>{"name"=>"Faraz", "parent_id"=>"", "project_id"=>"6", "started_on"=>"05.11.2015", "finished_on"=>"12.12.2015"}, "commit"=>"Absenden", "id"=>"33"} 
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]] 
Project Load (0.3ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", 6]] 
WorkPackage Load (0.3ms) SELECT "work_packages".* FROM "work_packages" WHERE "work_packages"."id" = $1 LIMIT 1 [["id", 33]] 
Unpermitted parameters: utf8, _method, authenticity_token, work_package, commit, id 
(0.1ms) BEGIN 
(0.1ms) COMMIT 
Redirected to http://localhost:3000/projects/6 
Completed 302 Found in 6ms (ActiveRecord: 0.9ms) 

你有什麼想法嗎?

enter image description here

+1

嘗試使用'filter_work_package_params'的'更新'方法。 – Pavan

回答

2

這是錯誤:

Unpermitted parameters: utf8, _method, authenticity_token, work_package, commit, id


你需要這樣做:

def filter_update_work_package_params 
    params.require(:work_package).permit(:package_id, :project_id, :team_id, :person_month, :started_on, :finished_on) 
end 

這將允許未經許可work_package參數,並應解決你的問題。

-

您也可以解決你的一些其他代碼太:

def edit 
    @work_package = WorkPackage.find(params[:package]) 
    redirect_to projects_path, danger: I18n.t('messages.errors.no_admin_rights_for_site') if !current_user.admin? || !own_project?(@project) 
end 

def update 
    @work_package = WorkPackage.find(params[:id]) 
    if @work_package.update filter_update_work_package_params 
    redirect_to project_path(@work_package.project_id), success: updated_success_message(I18n.t('objects.work_package')) 
    else 
    @object = @work_package 
    render 'edit' 
    end 
end 
+0

爲什麼OP需要像這樣更改'filter_update_work_package_params',如果他有'filter_work_package_params'這樣做 – Pavan

+0

因爲他在他的代碼中調用'filter_update_work_package_params'。我不知道他的屬性,所以也許這兩個是不同的 –

+0

我相信他正在調用錯誤的方法,請參閱'params'散列中'name'存在,'filter_work_package_params'只有'name'。 – Pavan

0

在試試這個你filter_update_work_package_params

def filter_update_work_package_params 
    params.require(:work_package).permit(:package_id, :project_id, :team_id, :person_month, :started_on, :finished_on) 
end 
+1

爲什麼OP需要像這樣更改'filter_update_work_package_params',如果他具有相同的'filter_work_package_params'。 – Pavan

+0

請注意,'filter_update_work_package_params'具有不同的屬性。我在想,他需要不包含在'filter_work_package_params'中的其他屬性。 – 2015-11-06 09:01:11

+0

我相信他正在調用錯誤的方法,在'params'中看到'name'存在,'filter_work_package_params'只有名字。 – Pavan