【问题标题】:Rails nested form - refactor create action | cocoon gemRails 嵌套表单 - 重构创建动作 |茧宝石
【发布时间】:2016-05-30 11:01:12
【问题描述】:

一切正常,但我想将 create 操作中的代码更改为 update 操作中的代码。现在,在创建操作中,我正在遍历所有值并保存它们,但想在一行中完成。

我有一个大学模特。

class College< ActiveRecord::Base
   has_many :staffs, dependent: :destroy
   accepts_nested_attributes_for :staffs, reject_if: :all_blank, allow_destroy: true
end

这是我的 Staff.rb

class Staff < ActiveRecord::Base
  belongs_to :college
end

这些是我的 Staffs 控制器 createupdate 操作

def create
  @college= College.find(params[:college][:id_college_profile]
  )
  staff_params = params[:college][:staffs_attributes].values
  staff_params.each do |staff_param|
    @staff = @college.staffs.new
    @staff.name = staff_param[:name]
    @staff.designation = staff_param[:designation]
    @staff.experience = staff_param[:experience]
    @staff.specialization = staff_param[:specialization]
    @staff.save
  end
  redirect_to dashboard_path(id: @college.id), notice: "Successfully added Staff."
end

def update
  @college= College.find(params[:college][:id_college]
  )
  @college.update_attributes(staff_parameters)
  redirect_to root_path
end

这些都是强参数

def staff_parameters
  params.require(:college).permit(:id, staffs_attributes: [:id,  :name, :specialization, :experience, :designation, :_destroy])
end

有没有一种方法可以在创建操作中保存所有人员,而无需遍历所有值,而是像在更新操作中一样使用一行代码保存所有人员?

我已经在 StaffsController 创建操作中尝试过这个

def create
  @college= College.find(params[:college][:id_college]
  )
  @staff= @college.staffs.new(staff_parameters)
  @staff.save

  redirect_to dashboard_path(id: @college.id), notice: "Successfully added Staffs."
end

但它抛出了这个错误

unknown attribute 'staffs_attributes' for Staff.

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails nested-forms has-many cocoon-gem


    【解决方案1】:

    这是CollegesController,所以我假设create 操作也会创建新学院?

    所以在这种情况下,您的创建操作应该是这样的:

    def create
      @college = College.new(staff_parameters)
      if @college.save
        # succesfully created
      else
        # there was a validation error
      end
    end 
    

    请注意,通常我们会使用 college_parameters,因为根元素是 college,而且您不仅可以编辑嵌套的五线谱,还可能编辑来自大学的属性。

    如果学院总是已经存在(因为你正在做一个find),我有点困惑createupdate 之间的区别以及为什么不总是呈现edit 动作那案子?

    我有一个 demo-project 展示茧和嵌套属性。

    【讨论】:

    • 感谢您的意见。这是流程。用户注册。然后他被重定向到一个表格。提交后,将为该用户创建一个大学对象。这个大学对象有员工、课程等。所以在员工控制器中,我正在为那个特定的大学创建员工。我发布的创建操作在 StaffsController 中。
    • 在这种情况下,编辑和新建操作没有区别,因为您总是为大学渲染表单,并保存大学,对吧?您的路线可能会有点混乱,因为您不会创建或编辑单个员工,而是一堆,所以对我来说,大学管理员感觉更“标准”足智多谋。
    • 谢谢@nathanvda。我现在正在为 new 和 edit 呈现编辑表单。我将把代码移到 CollegesController。一个小问题。我也有大学的“课程”,就像“员工”一样。如果在 Colleges#create,我有 @college.update_attributes(staff_parameters),如何更改强参数,以便在编辑嵌套课程表单时它也允许“course_parameters”。
    • 如果要包含courses,我会先开始重命名它(只是college_parameters 而不是staffs_parametersstaffs_and_courses_parameters),然后添加courses_attributes。比如:params.require(:college).permit(:id, staffs_attributes: { ...}, courses_attributes: { .. })。很简单的恕我直言?请查看我分享的演示项目以获得更详细/详细的示例。
    • 谢谢@nathanvda
    【解决方案2】:

    您可以通过多种方式做到这一点。 “staff_parameters”方法引发了错误,因为您在创建操作中的 Staff 类和更新操作的大学类上调用它。做你想做的最简单的事情是复制人员参数方法强参数并复制它。将第二种方法命名为 create_staff 并将“params.require(:college)”部分更改为“params.require(:staff)”,其余部分保持不变。然后在您的创建操作中,您可以执行“college.staff(create_staff)”。我在我的手机上,所以格式不太好,哈哈,我把代码放在引号里。

    【讨论】:

    • 感谢输入,但有错误。 def create_staff params.require(:staff).permit(staffs_attributes: [:id, :name, :specialization, :experience, :designation, :_destroy]) end此代码抛出此错误param is missing or the value is empty: staff
    • 这是我提交表单时的参数{"utf8"=&gt;"✓", "authenticity_token"=&gt;"2JTUGSxkviYutVSkbmsNHfkAlHoxTOk70OPxsDTcyKlUKVFMJx5+xF9RU6sZiXwIQqfNmOpw1NqnjJzJtrTHXQ==", "college"=&gt;{"staffs_attributes"=&gt;{"1464614354720"=&gt;{"name"=&gt;"asdasd", "specialization"=&gt;"asdasd", "designation"=&gt;"asdasd", "experience"=&gt;"12", "_destroy"=&gt;"false"}}, "id_college"=&gt;"2"}, "commit"=&gt;"Save", "controller"=&gt;"staffs", "action"=&gt;"create"}
    • 在创建动作中,我有这个:@staff = @college.staffs.new(create_staff)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多