关于使用 "nested_form" gem 的未定义方法 "reflect_on_association"。 Rails 5
undefined method "reflect_on_association" on using "nested_form" gem. Rails 5
在我的项目中,我有 API 及其相关的前端部分。我正在使用 rails 版本 5。我正在创建两个模型。一个是用户,另一个是资格。关联如下。
class User
include Her::Model
include Her::FileUpload
has_file_upload :image_url
has_many :videos
has_many :qualifications
attributes :name, :email, :phone, :image_url, :contact_email, :about, :password, :password_confirmation
custom_post :sign_in, :refresh
custom_get :me
accepts_nested_attributes_for :qualifications
end
而且,资格模型是
class Qualification
include Her::Model
belongs_to :user
attributes :school, :degree, :grade, :start_year, :end_year
end
我在用户控制器中的强参数是
def user_params
user: params.require(:user).permit(:id, :name, :email, :phone, :contact_email, :about,:image_url,
{specialization_ids: []},
qualifications_attributes: [:id, :school, :degree, :grade, :start_year, :end_year, :_destroy]
end
我提供的用户表单 nested_form 是
<div class="row">
<div class="container">
<div class="signup-bg col-md-12">
<h3 style="color:white;"> General Info </h3>
<hr>
<%= simple_nested_form_for @user do |f| %>
<%= f.input :email, input_html: {class: "input-user-form"} %>
<%= f.input :name, input_html: {class: "input-user-form"} %>
<%= f.input :phone , input_html: {class: "input-user-form"}%>
<%= f.input :contact_email, input_html: {class: "input-user-form"} %>
<%= f.input :about, as: :text, input_html: {class: "input-user-form"}%>
<label class="control-label">Selected Specializations</label>
<ul class="list-group">
<% @user.specializations.each do |sp| %>
<li class="list-group-item tags">
<%= check_box_tag "user[specialization_ids][]", sp["id"], true%>
<%= sp["name"] %>
</li>
<% end %>
</ul>
<h4 style="color:white;"><center>Select Your Specializations</center></h4>
<div class="row">
<div class="col-md-12">
<div class="input-group">
<input id="searchText" type="text" class="input-signup" placeholder="Search Specialization" style="z-index:0;">
<span class="input-group-btn">
<button class="btn btn-default btn-go" type="button">Go!</button>
</span>
</div>
<div class="col-md-12 col-sm-12 col-lg-12" id="specializationHolder" style="min-height:85px;"></div>
</div>
</div>
<h3 style="color:white;"> Qualification Info </h3>
<hr>
<div id="qualification_holder">
<%= f.simple_fields_for :qualifications, @qualification do |g| %>
<%= g.input :school,input_html: {class: "input-user-form"} %>
<%= g.input :degree, input_html: {class: "input-user-form"} %>
<%= g.input :grade, as: :integer, input_html: {class: "input-user-form"} %>
<%= g.input :start_year, required: false, as: :string, input_html: {type: :date, class: "datepicker input-user-form"} %>
<%= g.input :end_year, required: false, as: :string, input_html: {type: :date, class: "datepicker input-user-form"} %>
<% end %>
</div>
</br>
<div class="col-md-4">
<%= f.link_to_add :qualifications, data: {target: "#qualification_holder"}, class: "btn-signup-submit" do %>
<b> + </b> Add Qualifications
<% end %>
</div>
<div id="wrapper">
<label class="control-label file optional" for="user_image_url">Profile Picture</label>
<input class="file optional" type="file" name="user[image_url]" id="fileUpload">
</div>
<div class="form-group text-center">
<button type="submit" class="btn-signup-submit">Save Changes</button>
</div>
<% end %>
</div>
</div>
</div>
但是,我收到错误未定义错误 "reflect_on_association",指向 "link_to_add"。我被困在这里一个星期了。有什么解决办法吗。。如果有,请帮帮我。
提前致谢。
我有同样的错误。解决方案是在有问题的关联上添加 class_name
选项,而不在 class 名称上嵌套。
你的情况,你可以试试:
class User
include Her::Model
include Her::FileUpload
has_file_upload :image_url
has_many :videos
has_many :qualifications, class_name: "::Qualification"
attributes :name, :email, :phone, :image_url, :contact_email, :about, :password, :password_confirmation
custom_post :sign_in, :refresh
custom_get :me
accepts_nested_attributes_for :qualifications
end
在我的项目中,我有 API 及其相关的前端部分。我正在使用 rails 版本 5。我正在创建两个模型。一个是用户,另一个是资格。关联如下。
class User
include Her::Model
include Her::FileUpload
has_file_upload :image_url
has_many :videos
has_many :qualifications
attributes :name, :email, :phone, :image_url, :contact_email, :about, :password, :password_confirmation
custom_post :sign_in, :refresh
custom_get :me
accepts_nested_attributes_for :qualifications
end
而且,资格模型是
class Qualification
include Her::Model
belongs_to :user
attributes :school, :degree, :grade, :start_year, :end_year
end
我在用户控制器中的强参数是
def user_params
user: params.require(:user).permit(:id, :name, :email, :phone, :contact_email, :about,:image_url,
{specialization_ids: []},
qualifications_attributes: [:id, :school, :degree, :grade, :start_year, :end_year, :_destroy]
end
我提供的用户表单 nested_form 是
<div class="row">
<div class="container">
<div class="signup-bg col-md-12">
<h3 style="color:white;"> General Info </h3>
<hr>
<%= simple_nested_form_for @user do |f| %>
<%= f.input :email, input_html: {class: "input-user-form"} %>
<%= f.input :name, input_html: {class: "input-user-form"} %>
<%= f.input :phone , input_html: {class: "input-user-form"}%>
<%= f.input :contact_email, input_html: {class: "input-user-form"} %>
<%= f.input :about, as: :text, input_html: {class: "input-user-form"}%>
<label class="control-label">Selected Specializations</label>
<ul class="list-group">
<% @user.specializations.each do |sp| %>
<li class="list-group-item tags">
<%= check_box_tag "user[specialization_ids][]", sp["id"], true%>
<%= sp["name"] %>
</li>
<% end %>
</ul>
<h4 style="color:white;"><center>Select Your Specializations</center></h4>
<div class="row">
<div class="col-md-12">
<div class="input-group">
<input id="searchText" type="text" class="input-signup" placeholder="Search Specialization" style="z-index:0;">
<span class="input-group-btn">
<button class="btn btn-default btn-go" type="button">Go!</button>
</span>
</div>
<div class="col-md-12 col-sm-12 col-lg-12" id="specializationHolder" style="min-height:85px;"></div>
</div>
</div>
<h3 style="color:white;"> Qualification Info </h3>
<hr>
<div id="qualification_holder">
<%= f.simple_fields_for :qualifications, @qualification do |g| %>
<%= g.input :school,input_html: {class: "input-user-form"} %>
<%= g.input :degree, input_html: {class: "input-user-form"} %>
<%= g.input :grade, as: :integer, input_html: {class: "input-user-form"} %>
<%= g.input :start_year, required: false, as: :string, input_html: {type: :date, class: "datepicker input-user-form"} %>
<%= g.input :end_year, required: false, as: :string, input_html: {type: :date, class: "datepicker input-user-form"} %>
<% end %>
</div>
</br>
<div class="col-md-4">
<%= f.link_to_add :qualifications, data: {target: "#qualification_holder"}, class: "btn-signup-submit" do %>
<b> + </b> Add Qualifications
<% end %>
</div>
<div id="wrapper">
<label class="control-label file optional" for="user_image_url">Profile Picture</label>
<input class="file optional" type="file" name="user[image_url]" id="fileUpload">
</div>
<div class="form-group text-center">
<button type="submit" class="btn-signup-submit">Save Changes</button>
</div>
<% end %>
</div>
</div>
</div>
但是,我收到错误未定义错误 "reflect_on_association",指向 "link_to_add"。我被困在这里一个星期了。有什么解决办法吗。。如果有,请帮帮我。
提前致谢。
我有同样的错误。解决方案是在有问题的关联上添加 class_name
选项,而不在 class 名称上嵌套。
你的情况,你可以试试:
class User
include Her::Model
include Her::FileUpload
has_file_upload :image_url
has_many :videos
has_many :qualifications, class_name: "::Qualification"
attributes :name, :email, :phone, :image_url, :contact_email, :about, :password, :password_confirmation
custom_post :sign_in, :refresh
custom_get :me
accepts_nested_attributes_for :qualifications
end