我是Ruby on rails和构建应用程序的新手,用户可以在注册后添加他的教育历史记录。我使用两个编辑表单,一个默认设计编辑视图用于密码或电子邮件更新,第二个编辑用户视图,我与users_controller一起创建用于更新其他字段,如名称等。我使用嵌套表单添加教育详细信息。问题是如何初始化教育模型,因为我希望用户在注册后添加教育详细信息。我不希望用户在注册字段中提示用户使用所有这些字段。 没有错误,但没有教育领域。
主要问题是我使用设计注册和自定义编辑表单来添加教育字段。我需要编写自定义新方法吗?
这是我的app / views / users / edit.html.erb
中的代码 <div class="input-field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="input-field">
<%= f.label :date_of_birth %><br />
<%=f.date_field(:date_of_birth, :class => "datepicker")%>
</div>
<div class="input-field">
<%= f.label :city %><br />
<%= f.text_field :city, autofocus: true %>
</div>
<div class="input-field">
<%= f.label :about_me %>
<%= f.text_area :about_me, autofocus: true %>
</div>
<%= f.fields_for :educations do |ff| %>
<div>
<%= ff.label :institution_name %>
<%= ff.text_field :institution_name %>
</div>
<div>
<%= ff.label :course %>
<%= ff.text_field :course %>
</div>
<div class="input-field">
<%= f.label :from %><br />
<%=f.date_field(:from, :class => "datepicker")%>
</div>
<div class="input-field">
<%= f.label :to %><br />
<%=f.date_field(:to, :class => "datepicker")%>
</div>
<% end %>
<%= f.submit "Save changes"%>
<% end %>
这是我的userss_controller.rb
class UsersController < ApplicationController
before_action :authenticate_user!
before_action :prepare_schools
before_action :correct_user, only: [:edit, :update]
def new
end
def index
@users = User.all
end
def show
@user= User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user==(@user)
end
private
def user_params
params.require(:user).permit(:name,:date_of_birth,:city,:about_me,
:educations_attributes => [:id,:institution_name, :from, :to])
end
def prepare_schools
@schools = School.all
end
end
here is my USER model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
enum user_type: [:student, :teacher, :principal, :other]
has_one :school
has_many :educations, dependent: :destroy, inverse_of: :user
accepts_nested_attributes_for :educations, :allow_destroy => true,
:reject_if => :all_blank,
:allow_destroy => true
validates :name, presence: true, length: { maximum: 50 }
validates :user_type, presence: true, length: { maximum: 50 }
validates :school_id, presence: true
validates :date_of_birth, presence: true
validates :about_me, length: { maximum: 1000 }
validates :city, length: { maximum: 100 }
end
这是我的教育模式(嵌套模型)
class Education < ActiveRecord::Base
belongs_to :user, inverse_of: :educations
validates_presence_of :user
end
编辑(由pavan建议):这是通过表单的参数,嵌套属性不存在
!ruby/hash:ActionController::Parameters
utf8: "✓"
_method: patch
authenticity_token:1gHK22TcGLsPjIdb/
user: !ruby/hash:ActionController::Parameters
name: ''
date_of_birth: '1992-03-03'
city: delhi
about_me: i am what i am!!
commit: Save changes
controller: users
action: update
id: '3'
P.S。我尝试使用formhelper函数,但这没有帮助。
答案 0 :(得分:0)
在new
或edit
操作中,您可以构建educations
个对象,它将初始化嵌套属性。如下 -
def new
current_user.eductions.build #or @user.educations.build
# it will initialize a eduction object in form
end