我有 User 模型(用于设计),然后我有引用 User 的 member ,然后是引用 member 的投资组合。
我在注册时创建了一个用户。现在我希望注册用户更新他的详细信息,即成员和投资组合表。投资组合模型是:-
class Portfolio < ApplicationRecord
belongs_to :member
validates_presence_of title:
end
class Member < ApplicationRecord
belongs_to :user
has_one :portfolio
accepts_nested_attributes_for :portfolio
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :member
validates_presence_of :name
validates :email, uniqueness: true
after_create :create_member_portfolio
def create_member_portfolio
puts "Test "
end
end
在我的 members_controller 中,我拥有的代码是:-
class MembersController < ApplicationController
before_action :set_member, only: [:show, :edit, :update, :destroy]
# GET /members
# GET /members.json
def index
@members = Member.all
end
# GET /members/1
# GET /members/1.json
def show
end
# GET /members/new
def new
@member = setup_member(Member.new)
end
# GET /members/1/edit
def edit
end
# POST /members
# POST /members.json
def create
p = member_params
byebug
@member = Member.new(p)
byebug
@member.user_id = current_user.id unless current_user.nil?
respond_to do |format|
if @member.save
format.html { redirect_to @member, notice: 'Member was successfully created.' }
format.json { render :show, status: :created, location: @member }
else
format.html { render :new }
format.json { render json: @member.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /members/1
# PATCH/PUT /members/1.json
def update
respond_to do |format|
if @member.update(member_params)
format.html { redirect_to @member, notice: 'Member was successfully updated.' }
format.json { render :show, status: :ok, location: @member }
else
format.html { render :edit }
format.json { render json: @member.errors, status: :unprocessable_entity }
end
end
end
# DELETE /members/1
# DELETE /members/1.json
def destroy
@member.destroy
respond_to do |format|
format.html { redirect_to members_url, notice: 'Member was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_member
@member = Member.find(params[:id])
end
def setup_member(member)
member.portfolio ||= Portfolio.new
member
end
# Only allow a list of trusted parameters through.
def member_params
# slice(*filter.keys).each{|k,v| puts "#{k}:#{v}"}
#byebug
params.require(:member).permit(
:dob,
:email,
:phone,
portfolio_attributes: [ :title, :subtitle ]
)
end
end
html.erb 如下所示:-
<%= form_for(@member) do |f| %>
<% if @member.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(member.errors.count, "error") %> prohibited this member from being saved:</h2>
<ul>
<% @member.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-control">
<%= f.label :dob %>
<%= f.datetime_select :dob %>
</div>
<div class="form-control">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="form-control">
<%= f.label :phone %>
<%= f.text_field :phone %>
</div>
<div class="col-md-12">
<h2>Profile Details :</h2>
<div>
<%= f.fields_for :portfolio do |ff| %>
<div class="form-control">
<%= ff.label :title %>
<%= ff.text_field :title %>
</div>
<div class="form-control">
<%= ff.label :subtitle %>
<%= ff.text_field :subtitle %>
</div>
<% end %>
</div>
</div>
<div class="form-group">
<%= f.submit 'Save Member Item', class: 'btn btn-primary btn-block' %>
</div>
<% end %>
但是,当在保存成员对象的创建控制器方法中它失败并说 Portfolio.member 不存在。尽管我在传入的参数中将portfolio_attributes 作为带有标题和副标题的键,并且这些也经过验证,但保存失败。你能告诉我我在这里做错了什么吗?
我在此处调用 adter member.save 的错误如下所示:-
byebug) @member.errors
#<ActiveModel::Errors:0x00007fa011bceba8 @base=#<Member id: nil, fname: "asdsa", lname: "asdfsda", dob: "2020-06-16 18:59:00", email: "asddfsda@asdfdsa.com", religion_id: nil, phone: "", created_at: nil, updated_at: nil, user_id: 1>, @messages={:"portfolio.member"=>["must exist"]}, @details={:"portfolio.member"=>[{:error=>:blank}]}>
(byebug)