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 8192317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:08:58+00:00 2026-06-07T04:08:58+00:00

How to map out has_one model with nested routes and how to add a

  • 0

How to map out has_one model with nested routes and how to add a form_for for /localhost:3000/users/1/profile/new,html.erb following RESTful database?

User has one Profile.

Models

class Profile < ActiveRecord::Base
  attr_accessible :name, :surname
  belongs_to :user
end

class User < ActiveRecord::Base
  attr_accessible :email, :email_confirmation, :password, :password_confirmation
  has_secure_password
  has_one :profile, dependent: :destroy
end

  resources :users do
    resources :profiles      (note: has_one profile)
    resources :progress_charts
    resources :calories_journals
  end

views/profiles/new.html.erb

<h1>About You</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(@profile) do |f| %>
    <%= render 'shared/error_messages' %>

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

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

      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

controller: Profiles_controller.rb
Two errors I have discovered as I do not quite understand why it wasn’t working.

class ProfilesController < ApplicationController
def index
end

def show
end

  def new
  #  @user = User.find(params[:id]) # Error message: Couldn't find User without an ID
  #  @profile = @user.build_profile()

    @profile = current_user.build_profile(params[:id]) # Error message: unknown attributes: user_id
  end

  def edit
  end

  def create
  end

  def update
  end

  def destroy
  end
end

Helpers: SessionHelper (to illustrate current_user)
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user?(user)
    user == current_user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end

  def signed_in_user
    unless signed_in?
      store_location
      redirect_to signin_path, notice: "Please sign in."
    end
  end
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-07T04:08:59+00:00Added an answer on June 7, 2026 at 4:08 am

    Does your profiles table has a user_id attribute?

    In your routes, profile should be singular, since a user has one profile:

    resources :users do
        resource  :profile
        resources :progress_charts
        resources :calories_journals
    end
    

    The route to a user profile will be users/:user_id/profile (and not users/:user_id/profile/:id

    In your profiles_controller:

    @profile = current_user.build_profile(params[:id]) # why params[:id]?
    #it should just be
    @profile = current_user.build_profile()
    @user = User.find(params[:user_id])
    

    And the form would be something like:

    form_for [@user, @profile] do |f|...
    end
    

    Do you really want the user to create his profile like that? Usually, you would create the profile when a user register.

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

Sidebar

Related Questions

I'm struggling on how to map out the model for a database I'm looking
I have Java Map (out of Strings and Ints) objects that I want to
I need to be able to map out in coordinates real world cities and
When I use in my application: <resources location=/resources/favicon.ico mapping=/favicon.ico /> to map out single
Whenever the user scrolls map or zooms in/out, this method gets called instantaneously. I
I'm trying to figure out the simplest way to map an xml file to
I'm having some problems trying to figure out how to generate a Google Map
I'd like to work out the distance between two points. No map. No javascript.
I am calling AsyncTask on zoom-in and zoom-out of google map after zoom-in or
I am showing POI's List on google map as:- 1- User Zoom-In or Zoom-Out

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.