我现在一直在努力争取数据为我的嵌套表单保存。我基本上希望能够存储用户取消项目的理由,以及在项目取消之前的项目的最后阶段。但我似乎无法让行动cancel
,cancel_save
和cancel_params
很好地发挥作用!
控制器
before_action :correct_user, only: [:show, :edit, :update, :destroy, :cancel, :cancel_save]
...
def cancel
@project.build_project_close_reason
end
def cancel_save
@project.build_project_close_reason(cancel_params)
@project.update(project_status_id: 10)
redirect_to root_path, notice: 'Project has been successfully cancelled.'
end
private
def correct_user
@user = current_user
@project = current_user.projects.find_by(id: params[:id])
end
redirect_to user_projects_path, notice: "You are not authorised to view this project" if @project.nil?
end
def cancel_params
params.require(:project).permit(project_close_reason_attributes: [:comment]).merge(project_close_reason_attributes: [project_id: @project.id, last_status_id: @project.project_status_id ])
end
模型
class Project < ApplicationRecord
belongs_to :user
has_one :project_close_reason
accepts_nested_attributes_for :project_close_reason #adding this seemed to make no difference?
end
class ProjectCloseReason < ApplicationRecord
belongs_to :project
end
class User < ApplicationRecord
... # All standard devise stuff
has_many :projects
end
视图中的嵌套表单
<%= form_for([@user, @project], url: {action: "cancel_save"}, method: :post) do |f| %>
<%= fields_for :project_close_reason do |pcr| %>
<div class="field entry_box">
<%= pcr.label "Why are you cancelling this project? (This helps us improve!)" %>
<%= pcr.text_area :comment, class: "form-control entry_field_text" %>
</div>
<% end %>
<div class="actions center space_big">
<%= f.submit "Cancel Project", class: "btn btn-lg btn-warning" %>
</div>
<% end %>
路线
devise_for :users
devise_for :admins
resources :users do
resources :projects do
get "cancel", :on => :member
post "cancel" => 'projects#cancel_save', :on => :member
end
end
这是我在尝试提交表单时遇到的错误:
ActionController::ParameterMissing in ProjectsController#cancel_save
param is missing or the value is empty: project
。它引用了cancel_params
操作
感谢您的帮助!
更新
这是我拨打cancel_save
Started POST "/users/2/projects/10/cancel" for ::1 at 2016-09-29 10:03:44 +0200
Processing by ProjectsController#cancel_save as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"h6K+VFyjW/dV189YOePWZm+Pmjey50xAMQIJb+c3dzpEaMv8Ckh3jQGOWfVdlfVH/FxolbB45fXvTO0cdplhkg==", "project_close_reason"=>{"comment"=>"b"}, "commit"=>"Cancel Project", "user_id"=>"2", "id"=>"10"}
User Load (11.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Project Load (0.7ms) SELECT "projects".* FROM "projects" WHERE "projects"."user_id" = $1 AND "projects"."id" = $2 LIMIT $3 [["user_id", 2], ["id", 10], ["LIMIT", 1]]
ProjectCloseReason Load (0.2ms) SELECT "project_close_reasons".* FROM "project_close_reasons" WHERE "project_close_reasons"."project_id" = $1 LIMIT $2 [["project_id", 10], ["LIMIT", 1]]
Completed 400 Bad Request in 22ms (ActiveRecord: 12.1ms)
ActionController::ParameterMissing (param is missing or the value is empty: project):
答案 0 :(得分:0)
该错误表示没有名称为project
的参数,您在params.require(:project)
的{{1}}行中尝试使用该参数。我认为这是因为cancel params
你提到form_for
,这意味着[@user, @project]
。所以user's project
。致电project params must be inside user's
时,请检查您的日志。必须有类似cancel_save
的内容。
答案 1 :(得分:0)
最后,我完成了它而不需要任何项目。这是我cancel_params
中修订的projects_controller
。对于其他任何想要进行这种添加剂数据库处理的人来说,如果你能这样做的话,我强烈建议你跳过嵌套形式的参数。 简单得多!
private
def cancel_params
params.require(:project_close_reason).permit(:comment).merge(project_id: @project.id, last_status_id: @project.project_status_id )
end