簡體   English   中英

即使在rails 5中將其列入白名單也不允許使用屬性

[英]unpermitted attributes even after whitelisting in rails 5

項目詳細信息-> rails 5.1和ruby 2.4.1

我正在嘗試創建一個簡單的待辦事項應用程序。 我的問題是嵌套的模型形式。 如果我創建一個沒有任何任務的項目並將其保存。 然后,如果我要編輯項目並添加一些任務,則不會顯示任務字段。 如果我在創建項目時添加任務,那么一切都會按預期進行。在編輯頁面中,我可以看到項目和任務,也可以編輯。

以下是我的2個型號。 我沒有使用嵌套路由。 僅使用嵌套的模型形式。

class Project < ApplicationRecord
   has_many :tasks, inverse_of: :project, dependent: :destroy
   validates :name, presence: :true
   validates :description, presence: :true
   accepts_nested_attributes_for :tasks, reject_if: proc { |attributes| attributes[:name].blank? }
end

class Task < ApplicationRecord
   belongs_to :project, inverse_of: :tasks
end

以下是我的_form部分,用於新建和編輯。

<%= form_for @project do |form| %>
        <% if @project.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
          <ul>
          <% @project.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
          </ul>
        </div>
      <% end %>

      <div class="field">
        <%= form.label :name %>
        <%= form.text_field :name, id: :project_name %>
      </div>

      <div class="field">
        <%= form.label :description %>
        <%= form.text_field :description, id: :project_description %>
      </div>

      <%= form.fields_for :tasks do |task_form| %>
        <div class="field">
          <%= task_form.label :task_name %>
          <%= task_form.text_field :name, id: :task_name %>
        </div>
        <div class="field">
          <%= task_form.label :task_description %>
          <%= task_form.text_field :description, id: :task_description %>
        </div>
      <% end %>

      <div class="actions">
        <%= form.submit  %>
      </div>
<% end %>

下面給出了項目負責人的白名單。

 def project_params
   params.require(:project).permit(:name, :description, tasks_attributes: [:id, :name, :description])
 end

任何幫助表示贊賞-Ajith

如果我創建一個沒有任何任務的項目並將其保存。 然后,如果我要編輯項目並添加一些任務,則不會顯示任務字段。

……實際上是這樣的。 因此,您的問題很可能與“強參數”問題無關,因為我在您的視圖文件和project_params方法中看不到任何明顯的問題。 現在,因為你不希望這種行為,你可能會想是它建立了1點或多個以下task默認的對象,如果沒有task相關對象project還沒有,所以總是會有至少一個創建或編輯project時的一組task字段。

應用程序/控制器/ projects_controller.rb

class ProjectsController < ApplicationController
  def new
    @project = Project.new
    @project.tasks.build
    # the above code just above is the same as below
    # @project.tasks << Task.new

    # now you can call this again like below, if you want 2 groups of `tasks` fields when you're creating a Project
    @project.tasks.build
  end

  def edit
    @project = Project.find(params[:id])

    # this project may not yet have a `Task` object associated to it; if so we build a `Task` object like so
    # this builds 3 `Task` objects; you can just use one below if you just want to show one in your edit form.
    if @project.tasks.count == 0
      @project.tasks.build
      @project.tasks.build
      @project.tasks.build
    end
  end
end

PS,如果您希望擁有一種可以自動為您構建動態嵌套屬性的表單(例如,您需要如下所示的2個按鈕)“(添加更多任務)”和“(刪除此任務),您可能會對cocoon寶石感興趣。 `

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM