具有强大参数的 Rails 6 嵌套资源

God*_*a74 2 ruby ruby-on-rails

我在我的 Rails 应用程序中嵌套了资源,基本上是一个Projecthas Targets,我认为创建关系的最简单方法是在我的 routes.rb 中执行此操作:

resource :projects do
  resource :targets
end
Run Code Online (Sandbox Code Playgroud)

我的模型也非常匹配:

- Project.rb
class Project < ApplicationRecord
  has_many :targets
end

- Target.rb
class Target < ApplicationRecord
  has_one :project
end
Run Code Online (Sandbox Code Playgroud)

跑步rake routes向我展示了我的期望:

project_targets GET    /projects/:project_id/targets(.:format)                                                  targets#index
                                      POST   /projects/:project_id/targets(.:format)                                                  targets#create
                   new_project_target GET    /projects/:project_id/targets/new(.:format)                                              targets#new
                  edit_project_target GET    /projects/:project_id/targets/:id/edit(.:format)                                         targets#edit
                       project_target GET    /projects/:project_id/targets/:id(.:format)                                              targets#show
                                      PATCH  /projects/:project_id/targets/:id(.:format)                                              targets#update
                                      PUT    /projects/:project_id/targets/:id(.:format)                                              targets#update
                                      DELETE /projects/:project_id/targets/:id(.:format)                                              targets#destroy
                             projects GET    /projects(.:format)                                                                      projects#index
                                      POST   /projects(.:format)                                                                      projects#create
                          new_project GET    /projects/new(.:format)                                                                  projects#new
                         edit_project GET    /projects/:id/edit(.:format)                                                             projects#edit
                              project GET    /projects/:id(.:format)                                                                  projects#show
                                      PATCH  /projects/:id(.:format)                                                                  projects#update
                                      PUT    /projects/:id(.:format)                                                                  projects#update
                                      DELETE /projects/:id(.:format)                                                                  projects#destroy
Run Code Online (Sandbox Code Playgroud)

完成此操作后,我的创建/编辑表单不再适用于target,因此我不得不调整form_forfrom的第一行:

<%= form_for @target do |f| %>

to

<%= form_for @target, url: project_targets_path do |f| %>
Run Code Online (Sandbox Code Playgroud)

注意我必须明确声明网址

Target控制器中的 create 方法非常基本:

def create
  @target = Target.create(target_params)
  if @target.valid?
    redirect_to project_target_path(id: @target.id)
  else
    flash[:errors] = @target.errors.full_messages
    redirect_to new
  end
end
Run Code Online (Sandbox Code Playgroud)

我尝试创建一个目标是成功的,但目标没有project_id分配给它,每个 db 架构:

create_table :targets do |t|
  t.string :domain
  t.boolean :investigated, default: false
  t.boolean :research, default: false
  t.integer :project_id
  t.timestamps
end
Run Code Online (Sandbox Code Playgroud)

这就是我在日志中看到的内容,显然它project_id是作为 URL 的一部分传递的,但它没有与创建时的新目标相关联。

Started POST "/projects/1/targets" for ::1 at 2020-03-13 19:19:57 -0400
Processing by TargetsController#create as HTML
  Parameters: {"authenticity_token"=>"jLzQmpsxMRW4z66WguFVwZcLnMxFJYIy86EDfru6fIhysWDU/fd6yq5HV2uv1Z3TICGQSAXZDll66DwizReWaQ==", "target"=>{"domain"=>"2-test.com"}, "commit"=>"Create", "project_id"=>"1"}
   (0.1ms)  begin transaction
  ? app/controllers/targets_controller.rb:11:in `create'
  Target Create (0.8ms)  INSERT INTO "targets" ("domain", "created_at", "updated_at") VALUES (?, ?, ?)  [["domain", "2-test.com"], ["created_at", "2020-03-13 23:19:57.097233"], ["updated_at", "2020-03-13 23:19:57.097233"]]
  ? app/controllers/targets_controller.rb:11:in `create'
   (1.1ms)  commit transaction
  ? app/controllers/targets_controller.rb:11:in `create'
Run Code Online (Sandbox Code Playgroud)

我的设置不正确吗?我该如何解决这个问题,以便在创建新目标时project_id包括在内?如果可能的话,我想保留这个 RESTful 并且在 project_id 中不传递隐藏字段。

max*_*max 5

将表格设置为:

<%= form_for [@project, @target] do |f| %>
Run Code Online (Sandbox Code Playgroud)

虽然您可以显式传递一个 url:

<%= form_for @target, url: project_targets_path(@project) do |f| %>
Run Code Online (Sandbox Code Playgroud)

如果您以部分形式共享表单,这将破坏editupdate操作,/projects/:project_id/targets/:id因为更新时操作属性应指向。更喜欢约定而不是配置。

你的 create 方法在很多方面也被破坏了。

class TargetsController < ApplicationController
  before_action :set_project
  before_action :set_target, except: [:new, :index]

  # POST /projects/1/targets
  def create
    # building the new record off the parent sets the project_id
    @target = @project.targets.new(target_params)
    if @target.save
      redirect_to [@project, @target] 
    else
      flash[:errors] = @target.errors.full_messages
      # When a record is invalid always render - never redirect.
      render :new
    end
  end

  private
  def set_project
    @project = Project.find(params[:project_id])
  end
  # ...
end
Run Code Online (Sandbox Code Playgroud)

if @project.valid?只检查应用程序验证是否已通过 - 而不是记录是否实际保存到数据库中。检查@target.saveor的返回值@target.persisted?

请注意,:project_id不应包含在您的参数白名单中,因为它通过 url 而不是通过批量分配。嵌套路由确实与强参数没有任何关系。

您还应该阅读浅层嵌套,因为您很可能不需要嵌套成员路由,并且它大大降低了复杂性。