Ruby on rails Rails-嵌套强参数
我正在尝试使用两个模型user和profile创建一个用户寄存器,在一个控制器中嵌套强参数。当我发送参数时,我得到用户的未知属性“profiles\u attributes”的错误。我无法创建用户配置文件:Ruby on rails Rails-嵌套强参数,ruby-on-rails,nested-attributes,strong-parameters,Ruby On Rails,Nested Attributes,Strong Parameters,我正在尝试使用两个模型user和profile创建一个用户寄存器,在一个控制器中嵌套强参数。当我发送参数时,我得到用户的未知属性“profiles\u attributes”的错误。我无法创建用户配置文件: class User < ActiveRecord::Base has_one :profile has_many :apartments has_many :session has_secure_password validates :email, presence: t
class User < ActiveRecord::Base
has_one :profile
has_many :apartments
has_many :session
has_secure_password
validates :email, presence: true, uniqueness: true
validates :password, presence: true
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
belongs_to :city
has_many :profile_universities
has_many :universities, through: :profile_universities
has_many :profile_preferences
has_many :preferences, through: :profile_preferences
has_one :photo, :as => :imageable
end
class Api::V1::UserController < ApplicationController
before_action :user_params
def create_without_facebook
@user= User.new(user_params)
if @user.save
@profile = Profile.new(user_params[:profiles_attributes])
render json: [@user, @profile]
else
render json: @user.errors, status: :unprocessable_entity
end
end
def user_params
params.require(:user).permit(:email, :password, profiles_attributes: [:first_name, :last_name, :birthday, :gender, :marital_status, :ocupation, :budget, :question, :about, :city])
end
end
class用户:imageable
结束
类Api::V1::UserController
使用单数配置文件属性
如果是有一个
使用单数配置文件属性
如果是有一个
我做了此更改,创建了用户,但没有创建配置文件。这是回复:[{“id”:55,“电子邮件”:safe77@jdisji.com“,”密码摘要“$2a$10$OZ1lvo7xXMVILl1uN3ArWePhUDuNOADR8Uve3ao9EY/f1MHu1vBQK“,”创建于“:”2015-07-03T01:44:04.344Z”,“更新于“:”2015-07-03T01:44:04.344Z”},{“id”:null,“first_name”:null,“last_name”:null,“生日”:null,“性别”:null,“婚姻状况”:null,“职业”:null,“预算”:null,“问题”:null,“关于”:null,“用户\ id”:null,“城市\ id”:null,“个人资料\大学\ id”:null,“创建\在”:null,“更新\在”:null}]注释中的代码格式很糟糕:(如果你尝试了某件事,但它不起作用-你能重新处理这个问题吗?或者问一个新问题(如果你想,可以从这里指向它)。注意:您可能需要更新代码中的多个位置,也可能需要更新表单模板,以实现此目的-如果它们都使用了profile_属性…@SaulFelipeRamirez-Taryn的答案是正确的。您应该在user_-params方法中使用profile_属性。确保在html表单中也有此更改。要有效地调试此项,请使用ebug并检查create操作中user_params方法的输出。我做了此更改,创建了用户,但没有创建配置文件。这是响应:[{“id”:55,“email”:safe77@jdisji.com“,”密码摘要“$2a$10$oz1lvo7xxmvill1un3arwephudunoaadr8uve3ao9ey/f1MHu1vBQK”,“创建于“:”2015-07-03T01:44:04.344Z”,“更新于“:”2015-07-03T01:44:04.344Z”{“id”:null,“first_name”:null,“last_name”:null,“生日”:null,“性别”:null,“婚姻状况”:null,“职业”:null,“预算”:null,“问题”:null,“关于”:null,“用户id”:null,“城市id”:null,“概况大学id”:null,“创建地点”:null,“更新地点”:null}]注释中的代码格式非常糟糕:(如果你尝试了某件事,但没有成功-你能重做这个问题吗?或者问一个新问题(如果你愿意,可以从这里指向它)。注意:您可能需要更新代码中的多个位置,也可能需要更新表单模板,以实现此目的-如果它们都使用了profile_属性…@SaulFelipeRamirez-Taryn的答案是正确的。您应该在user_-params方法中使用profile_属性。确保在html表单中也有此更改。要有效地调试此项,请使用ebug并检查create操作中用户参数方法的输出。