gpt4 book ai didi

ruby-on-rails - Rails 嵌套表单不保存子项

转载 作者:行者123 更新时间:2023-12-04 06:14:26 25 4
gpt4 key购买 nike

我的用户模型 has_many 响应。我正在尝试创建一个嵌套表单来创建一个具有三个子响应的新用户。我的代码看起来与 Rails 类型转换相同,但虽然它会保存用户,但它不会保存他们的响应。任何人都可以看到有什么问题吗?

用户 Controller .rb

class UsersController < ApplicationController
def new
@user = User.new
3.times{@user.responses.build}
@responses = @user.responses
end

def create
@user = User.new(user_params)
if @user.save
redirect_to @user #todo: where do we want to redirect?
else
render 'new'
end
end

def show
@user = User.find(params[:id])
@responses = @user.responses
end

def index
@users = User.all
end

private

def user_params
params.require(:user).permit(:username, :email, :responses)
end
end

用户名
class User < ActiveRecord::Base
attr_accessor :responses_attributes
has_many :responses, :dependent => :destroy
accepts_nested_attributes_for :responses#, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

before_save { self.email = email.downcase }

validates :username, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX } ,
uniqueness: {case_sensitive: false};

validates :responses, presence: true
end

响应.rb
class Response < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :content
end

users/new.html.erb(表单)
<h1>This is a test form for adding Users with child Responses</h1>

<%= form_for @user do |f| %>
<%= render 'shared/error_messages' %>

<%= f.label :username %>
<%= f.text_field :username %>

<%= f.label :email %>
<%= f.text_field :email %>
<p>
<%= f.fields_for :responses do |builder| %>
<%= builder.label :content, "Response" %>
<%= builder.text_field :content %>
<% end %>
</p>
<%= f.submit "Submit" %>
<% end %>

编辑:

我已将用户 Controller 中的强参数更改为:
def user_params
params.require(:user).permit(:username, :email, responses_attributes: [:content])
end

我已将用户模型更新为:
class User < ActiveRecord::Base
has_many :responses, :dependent => :destroy
accepts_nested_attributes_for :responses#, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

before_save { self.email = email.downcase }

validates :username, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX } ,
uniqueness: {case_sensitive: false};

validates_associated :responses, presence: true
end

现在它继续使我对响应的验证失败,并且我对用户名和电子邮件的验证也失败了。

最佳答案

问题出在您的 Response 类中:

validates_presence_of :user_id, :content

此验证需要存在 user_id ,这意味着必须在响应之前创建用户。为了使其工作,您可以尝试以下几件事:
  • 更改 validates_presence_of :user_idvalidates_presence_of :user使其验证 User对象而不是用户的 id。
  • 使用 inverse_of选项。见 Rails API doc .

  • 更新了代码。

    user.rb
    class User < ActiveRecord::Base
    attr_accessible :responses_attributes
    has_many :responses, :dependent => :destroy, :inverse_of => :user
    accepts_nested_attributes_for :responses

    # ...
    end

    response.rb
    class Response < ActiveRecord::Base
    belongs_to :user, :inverse_of => :responses
    validates_presence_of :user, :content
    end

    关于ruby-on-rails - Rails 嵌套表单不保存子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21346910/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com