我有一个User模型(用于devise),然后我有一个引用User的成员,然后有一个引用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作为键,并且这些参数也经过了验证,但是保存失败。您能否让我知道我在这里做错了什么?
也许您可以通过检查来了解哪个字段导致了错误:
@member = Member.new(p)
@member.valid?
@member.errors
以下是有关它的一些文档:https://guides.rubyonrails.org/v2.3.11/activerecord_validations_callbacks.html#valid-and-invalid