ruby on rails - Accepts nested attribute form for has_one not populating -


i created preference model manage email settings. right now, nested form not showing on actual page. tried series of different tactics, can't figure out.

i made sure had preferences locally set up, changed fields_for (@user.preference) didn't work, etc.

i'm using rails 4.2.2, ruby version 2.2.1.

schema

  create_table "preferences", force: :cascade |t|     t.datetime "created_at",                    null: false     t.datetime "updated_at",                    null: false     t.integer  "user_id",                       null: false     t.boolean  "new_match",      default: true, null: false     t.boolean  "accepted_match", default: true, null: false     t.boolean  "match_reminder", default: true, null: false     t.boolean  "new_message",    default: true, null: false   end 

user model

  has_one :preference, dependent: :destroy   accepts_nested_attributes_for :preference 

preference model

class preference < activerecord::base   belongs_to :user end 

actual form has contains partials nested form:

<%= form_for(@user, id: "editing-info") |f| %>   <ul class="tabs">     <li class="tab-link current" data-tab="tab-account">       <%= t("edit_user.tabs.account") %>     </li>     <li class="tab-link" data-tab="tab-interests">       <%= t("edit_user.tabs.interests") %>     </li>     <li class="tab-link" data-tab="tab-settings">       <%= t("edit_user.tabs.settings") %>     </li>   </ul>    <div class="tab-content current" id="tab-account">     <%= render "users/tabs/account", f: f %>   </div>      <div class="tab-content" id="tab-interests">     <%= render "users/tabs/interests", f: f %>   </div>    <div class="tab-content" id="tab-settings">     <%= render "users/tabs/settings", f: f %>   </div>    <div id="button-container">     <%= link_to 'cancel',       user_path(current_user),       data: { disable_with: "loading..." },       class: "cancel-desktop" %>      <%= f.submit "save",       data: { disable_with: "loading..." },       class: "save main-save-button-edit" %>   </div> <% end %> 

partial includes nested form

<div id="settings">   <h4 class="notifications-label">notifications</h4>   <div class="notifications setting-category info-holder">     <div class="mobile setting-holder">       <%= f.label :mobile, "mobile", class: "setting-label" %>       <%= f.text_area :mobile, placeholder: "add mobile number", class: "setting-input" %>     </div>     <div class="email-holder setting-holder main-email-edit">       <%= f.label :email, "email", class: "setting-label" %>       <%= f.text_area :email, placeholder: "add email address", class: "setting-input" %>     </div>     <p class="setting-descrip">this email glassbreakers notifications sent. please continue use email associated linkedin account sign in.</p>   </div>   <h4 class="email-label">emails</h4>   <%= f.fields_for :preference |user_preference| %>     <div class="email-notifications-holder">       <div class="email-holder">         <%= user_preference.label :new_match, "getting new match each week" %>         <%= user_preference.check_box :new_match, class: "checkbox new_match_email" %>       </div>       <div class="email-holder">         <%= user_preference.label :match_reminder, "new matches thursday reminder", class: "match_reminder_email" %>         <%= user_preference.check_box :match_reminder, class: "checkbox thursday_reminder_email"   %>       </div>       <div class="email-holder">         <%= user_preference.label :accepted_match, "a glassbreakers accepted match", class: "accepted_match_email" %>         <%= user_preference.check_box :accepted_match, class: "checkbox accept_match_email"   %>       </div>       <div class="email-holder">         <%= user_preference.label :new_message, "received new message" %>         <%= user_preference.check_box :new_message, class: "checkbox new_message_email"  %>       </div>     </div>   <% end %>   <h4 class="connect-label">connections</h4>   <div class="connections info-holder">     <p class="inform">you'll receive new matches every tuesday. make sure respond matches before then.</p>     <div class="pausing">       <label><%= t("edit_user.settings.pausing_header") %></label>       <%= react_component(         "matchingpausedsetting",         {           matchingpaused: current_user.matching_paused?,           pausematchingurl: user_matching_pauses_path(current_user)         }       ) %>     </div>     <p class="setting-descrip pause-descrip">pausing connections means not receive new potential connections each week. able send , receive new messages.</p>   </div>   <h4 class="contact-label">contact</h4>   <div id="contact-info" class="info-holder">     <p class="inform">have question?  love hearing you. contact courtney, our communication manager, @ <a href="mailto:info@glassbreakers.co" target="_top">info@glassbreakers.co</a>.</p>   </div> </div> 

user controller pertaining user_params

  def user_params     filtered_params = params.require(:user).permit(       :email,       :mobile,       :bio_because,       :bio_meet,       :description,       :github_link,       :linkedin_link,       :title,       :twitter_link,       preference_attributes: [         :id,         :new_match,         :accepted_match,         :match_reminder,         :new_message,       ],       user_onboard_skills_attributes: [         :skill_id,         :industry_id,         :years_experience,         :id,         :_destroy,       ],       interest_ids: [],     )      filtered_params[:user_onboard_skills_attributes] =       filter_user_onboard_skills_attributes(         filtered_params[:user_onboard_skills_attributes]       )      filtered_params   end 

if nested form within user form itself, adjusting fields_for declaration should trick:

<%= f.fields_for @user.preference |preference_fields| %> 

also note renamed block variable preference_fields f, looks using f parent form.

the documentation has great explanation of method, should answer additional questions:

fields_for documentation

edit posterity:

the fields weren't correctly displaying because user model did not have associated preference model, can fixed manually associating preference model user, or utilizing strategy similar 1 found in question: rails: create association if none found avoid nil errors. pre-cautions taken @ model level ensure association created whenever new user created.


Comments