Rails 4 has_one关联形式没有建立

Was*_*per 14 ruby-on-rails form-for actioncontroller strong-parameters ruby-on-rails-4

我需要一些关于Rails 4如何与has_one和belongs_to关联一起工作的指针.

我的表格没有保存has_one关系

发布模型

class Post < ActiveRecord::Base
  validates: :body, presence: true

  has_one :category, dependent: :destroy
  accepts_nested_attributes_for :category
end

class Category < ActiveRecord::Base
  validates :title, presence: true
  belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)

后控制器

class PostController < ApplicationController
  def new
    @post = Post.new
    @post.build_category
  end

  def create
    @post = Post.new(post_params)
  end

  private

  def post_params
    params.require(:post).permit(:body)
  end
end
Run Code Online (Sandbox Code Playgroud)

表格在Post#new action中

<%= form_for @post do |form| %>

  <%= form.label :body %>
  <%= form.text_area :body %>

  <%= fields_for :category do |category_fields| %>
    <%= category_fields.label :title %>
    <%= category_fields.text_field :title %>
  <% end %>

  <%= form.button "Add Post" %>

<% end %>
Run Code Online (Sandbox Code Playgroud)

category提交帖子表单时,它不会保存标题.

调试参数

utf8: ?
authenticity_token: 08/I6MsYjNUhzg4W+9SWuvXbSdN7WX2x6l2TmNwRl40=
post: !ruby/hash:ActionController::Parameters
  body: 'The best ice cream sandwich ever'
category: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  title: 'Cold Treats'
button: ''
action: create
controller: posts
Run Code Online (Sandbox Code Playgroud)

应用日志

Processing by BusinessesController#create as HTML
  Parameters: {"utf8"=>"?",
               "authenticity_token"=>"08/I6MsYjNUhzg4W+9SWuvXbSdN7WX2x6l2TmNwRl40=",
               "post"=>{"body"=>"The best ice cream sandwich ever"},
               "category"=>{"title"=>"Cold Treats", "button"=>""}
Run Code Online (Sandbox Code Playgroud)

在Rails控制台中..我可以成功运行以下命令

> a = Post.new
=> #<Post id: nil, body: "">
> a.category
=> nil

> b = Post.new
=> #<Post id: nil, body: "">
> b.build_category
=> #<Post id: nil, title: nil>
> b.body = "The best ice cream sandwich ever"
=> "The best ice cream sandwich ever"
> b.category.title = "Cold Treats"
=> "Cold Treats"
Run Code Online (Sandbox Code Playgroud)

我的问题涉及如何解决这个问题:

  1. 我不知道如果我要加:category_attributespost_params强参数的方法?
  2. 日志和调试参数是否应该显示Category属性嵌套在Post参数内?
  3. Categoryhash参数中有一个空白button键不在我的fields_for身上,在使用表单助手时我遗漏了什么?
  4. 是因为创建操作不采用该 build_category方法,我需要将其添加到创建操作中吗?
  5. 将验证 Categorypresence: truePost表单上自动使用模型()?

提前致谢.

更新:遗失 category_fieldsfields_for块内.

sle*_*led 16

问题1:是的,您需要:category_attributespost_params强参数方法中添加如下:

def post_params
  params.require(:post).permit(:body, category_attributes: [:title])
end
Run Code Online (Sandbox Code Playgroud)

问题2:是的,参数应该是嵌套的,这是你视图中的拼写错误,因为你没有fields_for在父表单构建器的范围内应用(复数),也没有使用category_fields里面的表单构建器fields_for块!

视图应如下所示:

<%= form_for @post do |form| %>

  <%= form.label :body %>
  <%= form.text_area :body %>

  <%= form.fields_for :category do |category_fields| %>
    <%= category_fields.label :title %>
    <%= category_fields.text_field :title %>
  <% end %>

  <%= form.button "Add Post" %>

<% end %>
Run Code Online (Sandbox Code Playgroud)

问题3:由于视图中混合的表单构建,按钮参数可能位于错误的位置.

问题4:如果接受嵌套属性,则无需在创建操作中构建子模型

问题5:是的,也会运行子模型的验证,如果子项的验证失败,父项也会出错并且不会保存到数据库中.

  • 雪橇,你先生是个绅士.谢谢你指出我的错误方法.我一直在查看相同的表单,不知道我需要在fields_for块中添加form.builder.再次感谢您澄清强参数嵌套属性.我非常感谢你花时间回答我的所有问题.谢谢!:) (2认同)