ruby-on-railsruby-on-rails-5simple-formnested-forms

undefined method "reflect_on_association" on using "nested_form" gem. Rails 5


In my project I have API and its associated front end part. I am using rails version 5 . I am creating two models. One is user and another one is qualifications. Associations are given below.

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 

And, qualification model is

class Qualification
  include Her::Model

  belongs_to :user
  attributes :school, :degree, :grade, :start_year, :end_year
end

My strong params in users controller is

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

My user form, where i have provided nested_form is

<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>

But, I am getting an error undefined error "reflect_on_association", pointing towards "link_to_add". Is there a solution for this?


Solution

  • I had the same error. The solution is to add the class_name option on your problematic association, with no nesting on the class name.

    In your case, you can try:

    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