Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8560985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:24:02+00:00 2026-06-11T16:24:02+00:00

I am trying to have a nested form on my users/new page, where it

  • 0

I am trying to have a nested form on my users/new page, where it accepts user-attributes and also company-attributes. When you submit the form:

Here’s what my error message reads:

ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: companies
app/controllers/users_controller.rb:12:in `create'

Here’s the code for my form:

<%= form_for @user do |f| %>
      <%= render 'shared/error_messages', object: f.object %> 

      <%= f.fields_for :companies do |c| %>

      <%= c.label :name, "Company Name"%>
      <%= c.text_field :name %>

      <% end %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation %>
      <br>
      <% if current_page?(signup_path) %>
      <%= f.submit "Sign Up", class: "btn btn-large btn-primary" %>     Or, <%= link_to "Login", login_path %>
      <% else %>
      <%= f.submit "Update User", class: "btn btn-large btn-primary" %>
      <% end %>
<% end %>

Users Controller:

   class UsersController < ApplicationController

  def index
    @user = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.create(params[:user])
    if @user.save
      session[:user_id] = @user.id #once user account has been created, a session is not automatically created. This fixes that by setting their session id.  This could be put into Controller action to clean up duplication.
      flash[:success] = "Your account has been created!"
      redirect_to tasks_path
    else
       render 'new'
    end
  end

  def show
    @user = User.find(params[:id])
    @tasks = @user.tasks
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = @user.name.possessive + " profile has been updated"
      redirect_to @user
    else
      render 'edit'
    end

    #if @task.update_attributes params[:task]
    #redirect_to users_path
    #flash[:success] = "User was successfully updated."
    #end
end

  def destroy
    @user = User.find(params[:id])
    unless current_user == @user
      @user.destroy
      flash[:success] = "The User has been deleted."
    end
    redirect_to users_path
    flash[:error] = "Error. You can't delete yourself!"
  end

end

Company Controller

    class CompaniesController < ApplicationController

  def index
    @companies = Company.all
  end

  def new
    @company = Company.new
  end

  def edit
    @company = Company.find(params[:id])
  end

  def create
    @company = Company.create(params[:company])
    #if @company.save
      #session[:user_id] = @user.id #once user account has been created, a session is not automatically created. This fixes that by setting their session id.  This could be put into Controller action to clean up duplication.
      #flash[:success] = "Your account has been created!"
      #redirect_to tasks_path
    #else
       #render 'new'
    #end
  end

  def show
    @comnpany = Company.find(params[:id])
  end

end

User model

class User < ActiveRecord::Base
  has_secure_password

  attr_accessible :name, :email, :password, :password_confirmation
  has_many :tasks, dependent: :destroy
  belongs_to :company
  accepts_nested_attributes_for :company

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 6 }
  #below not needed anymore, due to has_secure_password
  #validates :password_confirmation, presence: true  
end

Company Model

    class Company < ActiveRecord::Base
  attr_accessible :name
  has_and_belongs_to_many :users
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:24:03+00:00Added an answer on June 11, 2026 at 4:24 pm

    First for debugging put the bang on the create in the two controllers like so: create! Your log may spit out more.

    Then, if that sucked, try it the old fashioned way of Building the two Objects and assigning each one with the params.

    I assume also that this is it for attributes, no after saves that should have more to the schema then you are showing.

    Lastly, you are missing

    def new
      @user = User.new
      @company = @user.companies.build
    end
    

    Print out the params too, just in case it says something wonky but adding this line: as @Beerlington said should work too :company_attributes, maybe companies_attributes… spit balling here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to submit a login form on a web page that looks
I have Python nested list that I'm trying to organize and eventually count number
I have a lot of nested dictionaries, I am trying to find a certain
I have an ASP Datalist with a nested GridView. I am trying to display
I have a navigation bar that has two dropdowns (as nested ul's). I'm trying
I am trying to work with a nested hash. I have a deck of
I am trying to remove items from a nested list in Python. I have
Trying to have my PHP script return some SQL table queries. Here's my script
I have a nested form in which I would like the form fields to
I have a server-generated html like: <ul> <li><!-- few nested elements that form a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.