使用“nested_form”gem时未定义的方法“reflect_on_association”。 Rails 5

时间:2017-10-13 06:29:05

标签: ruby-on-rails ruby-on-rails-5 simple-form nested-forms

在我的项目中,我有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”。我被困在这里一个星期。有没有解决方案..如果有,请帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我有同样的错误。解决方案是在有问题的关联上添加class_name选项,而不在类名上嵌套。

在您的情况下,您可以尝试:

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