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

  • Home
  • SEARCH
  • 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 8481705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:37:50+00:00 2026-06-10T19:37:50+00:00

I have two models user and profile. I want to save the username and

  • 0

I have two models user and profile.
I want to save the username and password in user and other user profile details in
profile.
Now,
The user model has:

has_one :profile
accepts_nested_attributes_for :profile
attr_accessible :email, :password,:profile_attributes

The profile model has

 belongs_to :user
 attr_accessible :firstname, :lastname

The user controller has

 def new
    @user = User.new
    @profileattr = @user.build_profile
  end

  def create
    @user = User.new(params[:user])

    if @user.save
      redirect_to root_url, :notice => "user created successfully!"
    else
      render "new"
    end
  end

The view new.html.erb have fields for both user and profile.
view has:

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% for message in @user.errors.full_messages %>
          <li><%= message %></li>
        <% end %>

      </ul>
    </div>
  <% end %>


  <p>
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </p>
  <p>
    <%= f.label :password %><br />
    <%= f.password_field :password %>
  </p>


  <%= f.fields_for @profileattr do |pf|%>
  <p>
    <%= pf.label :firstname %><br />
    <%= pf.text_field :firstname %>
  </p>
  <p>
    <%= pf.label :lastname %><br />
    <%= pf.text_field :lastname %>
  </p>
  <% end %>

  <p class="button"><%= f.submit %></p>
<% end %>

However,when I run this web application it shows error:

Can’t mass-assign protected attributes: profile

on debug I found the user has attributes as:

 :email:abc@gmail.com
 :password:secret

 :profile
            ---:firstname:abc
            ---:lastname:xyz

so,instead of expected params[:profile_attributes] the view returning params[:profile]

leading to mass-assignment error.so what is going wrong?

  • 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-10T19:37:52+00:00Added an answer on June 10, 2026 at 7:37 pm

    Did you try

      <%= f.fields_for :profile do |pf|%>
    

    ?

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

Sidebar

Related Questions

I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I currently have two different models: User and Project . The User model has
Two models (Rails 2.3.8): User; username & disabled properties; User has_one :profile Profile; full_name
I have created two models :User and UserProfile , Now I want display in
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
I have two models: User and Teacher : class User < ActiveRecord::Base attr_accessor :password
Given these two models: class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name=_('user')) about = models.TextField(_('about'),
I have two models: User and Base. User model: http://pastebin.com/WdLzBkHJ Base model: http://pastebin.com/tQrEUaSu At
I have a User model with name, email, password, and bio. I want to
I have two models, User and Profile, in one-to-one relation and I am trying

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.