1

I'm trying to create a new record for a model that belongs to my main model on the main model's update page, but it is not being saved to the database. Basically the Company model acts as the main user model, and it has the ability to create new board members for itself on its edit registration page that devise generates. Here is what I have.

1) my company model which has many boardmembers

class Company < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
    has_many :boardmembers
    accepts_nested_attributes_for :boardmembers
end

2) my boardmembers model

class Boardmember < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
    belongs_to :company
end

3) the companies controller

class Companies::RegistrationsController     < Devise::RegistrationsController
    prepend_before_filter :require_no_authentication, only: [ :new, :create, :cancel ]
    prepend_before_filter :authenticate_scope!, only: [:edit, :update, :destroy]


def create
    build_resource(sign_up_params)
    if resource.save
        redirect_to edit_company_registration_path
    else
        clean_up_passwords resource
        respond_with resource
    end
end


  def update
     # For Rails 4
     account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
      # For Rails 3
     # account_update_params = params[:user]

     # required for settings form to submit when password is left blank
     if account_update_params[:password].blank?
       account_update_params.delete("password")
       account_update_params.delete("password_confirmation")
     end

     @company = Company.find(current_company.id)

     # saves the companies boardmembers
     if @company.update_attributes(account_update_params)
       set_flash_message :notice, :updated
       # Sign in the user bypassing validation in case his password changed
       sign_in @company, :bypass => true
       redirect_to company_home_path
     else
       render 'edit'
     end
   end

end

3) and my application controller where i configure params

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) do |u|
      u.permit(:email, :name, :address, :jurisdiction_of_incorporation, :entity_type, :password,            :password_confirmation, boardmembers_attributes: [:company_id, :id, :email, :name, :address, :secondary_email,   :primary_phone, :password])
    end
    devise_parameter_sanitizer.for(:sign_up) do |u|
      u.permit(:email, :name, :password, :password_confirmation)
   end
end

end

My forms look something like this: edit.html.erb

 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
     <!-- here is where all the company update fields go -->
           ...
    <!-- here are the fields for creating boardmembers
    <%= f.fields_for :boardmembers, resource.boardmembers.build do |boardmember| %>

          <%= boardmember.text_field :name %>

            <%= boardmember.text_field :address %>


            <%= boardmember.text_field :primary_phone %>

            <%= boardmember.text_field :email %>

            <%= boardmember.text_field :secondary_email %>

            <%= boardmember.password_field :password %>
         <% end %>
   <%= f.submit "Update Your Account" %>

<% end %>

However the company gets its records updated but the new boardmember is not created at all. I even tried rendering a json file of the params when i click the button and it ends up looking like

   {"utf8":"✓","_method":"put","authenticity_token":"mK4yd8t4m7N5rdfmHG8XKc/c+vNUdO8vryk5kYm7juw=","company":   {"email":"pizzahut@email.com","name":"Pizza Comp","password":"","password_confirmation":"","entity_type":"","jurisdiction_of_incorporation":"","address":"","boardmembers_attributes":{"0":{"name":"","address":"","primary_phone":"","email":"","secondary_email":"","password":""}}},"commit":"Update Your Account","action":"update","controller":"companies/registrations"}

All of the params for the boardmembers are empty even when I fill them in. I've tried every tutorial and answer online and none of them seem to work in this case. What else could it be? Any ideas? The new record is never created. Please help.

user3716707
  • 11
  • 1
  • 2

1 Answers1

0

Try this

<%= form_for([resource,resource.with_boardmembers], as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>

        <!-- here is where all the company update fields go -->

       ...
       <!-- here are the fields for creating boardmembers

      <%= f.fields_for :boardmembers do |boardmember| %>

      <%= boardmember.text_field :name %>

        <%= boardmember.text_field :address %>


        <%= boardmember.text_field :primary_phone %>

        <%= boardmember.text_field :email %>

        <%= boardmember.text_field :secondary_email %>

        <%= boardmember.password_field :password %>
     <% end %>

In your User model add

def with_boardmembers
   self.boardmembers.build
   self
end
dips
  • 370
  • 6
  • 17
  • I referred from this - http://stackoverflow.com/questions/3544265/how-do-i-use-nested-attributes-with-the-devise-model – dips Jun 07 '14 at 08:02
  • 1
    Update: Worked. Just a syntax error I had was screwing it up – user3716707 Jun 07 '14 at 19:10
  • Your solution worked for one model but when I try to do it for another I get this http://stackoverflow.com/questions/24125366/weird-error-with-multiple-nested-attributes-in-rails What can I do? – user3716707 Jun 10 '14 at 14:46