簡體   English   中英

在帶有新父級和子級的表單上進行Rails模型驗證

[英]Rails model validation on form with new parent and child

我是Rails的新手,但現在想知道,當我嘗試同時創建子代和父代時,在模型對象上丟失了Rails驗證嗎?

在我的示例中(粗體顯示模型)1- 蛋糕 -has_many - CakeDetails

-CakeDetails-屬於1- 大小

1- CakeType-屬於1- Cake

我設法創建了一個表格,該表格使我可以創建蛋糕,指定尺寸,關聯的價格以及蛋糕的類型。

但是似乎所有內置的模型驗證都已消失。

這個對嗎? 如何將驗證帶回模型對象?

當我渲染新動作時,我彈出一個空白表格,當我提交一個空白表格時,我得到以下信息:

Started POST "/cakes" for 127.0.0.1 at 2018-04-25 21:19:03 +0100
Processing by CakesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"r6B2ult7w7z4vez95f2pNXmk7Q1rYSzj8Wi9VwnS4EgIAw8PG/w7isfAeiiSB9sY8+0+oHldMVpy1GFKBmrpmw==", "cake"=>{"name"=>"", "description"=>"", "cake_type_id"=>"", "cake_details_attributes"=>{"0"=>{"_destroy"=>"true", "price"=>""}, "1"=>{"_destroy"=>"true", "price"=>""}}}, "commit"=>"Create Cake"}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
  Rendering cakes/new.html.erb within layouts/application
  CakeType Load (0.3ms)  SELECT "cake_types".* FROM "cake_types"
  Rendered cakes/_form.html.erb (15.9ms)
  Rendered cakes/new.html.erb within layouts/application (19.6ms)
Completed 200 OK in 109ms (Views: 100.4ms | ActiveRecord: 0.5ms)

我得到它回滾事務,因為沒有cake_type_id。 但是沒有顯示錯誤。

以下是我表單中的錯誤顯示部分。 我嘗試添加更多內容以檢查嵌套對象中的錯誤,但什么也沒看到。

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

我的蛋糕模型

class Cake < ApplicationRecord
  validates :name, :description, :cake_type, presence: true

  belongs_to :cake_type
  has_many :cake_details, -> {order('size_id ASC')}, inverse_of: :cake
  has_many :sizes, through: :cake_details
  accepts_nested_attributes_for :cake_details, :allow_destroy => true

  validates_associated  :cake_type, :cake_details
end

蛋糕控制器

  def create
    #@cake = Cake.new(params[cake:{}])
    #params.require(:cake).permit(:name, :description,:cake_type_id)

    @cake = Cake.new(cake_params)
    respond_to do |format|
      if @cake.save
        format.html { redirect_to @cake, notice: 'Cake was successfully created.' }
        format.json { render :show, status: :created, location: @cake }
      else
        format.html { render :new }
        format.json { render json: @cake.errors, status: :unprocessable_entity }
      end
    end
  end

def cake_only_params
  params.require(:cake).permit(:name, :description,:cake_type_id)
end

def cake_params
  params.require(:cake).permit(:name, :description,:cake_type_id, cake_type_id:[:type_id],
  cake_details_attributes:[:_destroy, :id, :cake_id, :size_id, :price])
end

更新

我以為我必須訪問在操作中聲明的變量,例如@cake,但我更新了表單以使用此變量

<%= form_with(model: cake, local: true) do |form| %>

而不是我以為我需要它:

<%= form_with(model: @cake) do |form| %>

我會仔細閱讀文檔,但是對我為何搞砸了的任何評論絕對值得贊賞。 剛開始學習這個框架...:/

根據本文的介紹 ,我可以肯定在其他地方,在Rails 5中,默認情況下,所有使用form_with創建的form_with都是遠程的(即,以js形式提交)。

通過在form_with添加local: true ,您可以強制表單以html形式提交,然后您的控制器使用format.html

我以為我必須訪問我在操作中聲明的變量,例如

@蛋糕

但我更新了表單以使用此表單:

<%= form_with(model: cake, local: true) do |form| %>

而不是我以為我需要它:

<%= form_with(model: @cake) do |form| %>

現在可以了。

特別感謝jvillian如此迅速的回應。

並注意:

通過在form_with添加local: true ,您可以強制表單以html形式提交,然后您的控制器使用format.html

暫無
暫無

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

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