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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:18:49+00:00 2026-05-13T11:18:49+00:00

In my Ruby on Rails application I want to allow the adding/editing of a

  • 0

In my Ruby on Rails application I want to allow the adding/editing of a nested model which itself has an associated model.

model Survey
   string title
   has_many questions

model Question
   string question
   belongs_to category

model Category
   string name

For the sake of argument let’s assume that the user should always have to enter a new category when entering a question (I couldn’t come up with a better example, sigh).

In my model/survey/edit.html.erb I have a working setup for adding questions and saving them. However when I added the Category model to the picture, I now face the problem that when adding a new Question, there is no corresponding Category name-field displayed. I suspect this is because even though I do call Question.new, I do not call question.category.build – and I have no idea where/how to do that.

My edit.html.erb:

<h1>Editing Survey</h1>

<%= render :partial => 'form' %>

My _form.html.erb:

<% form_for(@survey) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </p>

  <div id="questions">
    <% f.fields_for :questions do |q| %>
      <%= render :partial => 'question', :locals => { :pf => q } %>
    <% end %>
  </div>

  <%= add_a_new_child_link("New question", f, :questions) %>
<% end %>

My _question.html.erb:

<div class="question">
  <%= pf.label :question %>
  <%= pf.text_field :question %>

  <% pf.fields_for :category do |c| %>
    <p>
      <%= c.label :name, "Category:" %>
      <%= c.text_field :name %>
    </p>
  <% end %>
</div>
  • 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-05-13T11:18:49+00:00Added an answer on May 13, 2026 at 11:18 am

    A quick fix for your situation is to use virtual attributes.
    EG, in your Question model:

    def category_name=(new_name)
     if category then
       category.name = new_name
     else
       category = Category.new(:name => new_name)
     end
    end
    
    def category_name
      return category.name if category 
      ""
    end
    

    In your _question, there no need to use nested form. Just add something like:

    <%= pf.text_field :category_name %>
    

    I didn’t test it, but you probably caught the ideea.

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

Sidebar

Related Questions

We're hosting our Ruby on Rails application with the EngineYard App Cloud, which has
I want to log user's actions in my Ruby on Rails application. So far,
After our Ruby on Rails application has run for a while, it starts throwing
I am modifying a Ruby Rails application - Redmine. I want to modify a
I want to automate things like: Creating a new Ruby on Rails application with
In my Ruby on Rails application I want to create a new profile and
In my Ruby on Rails application I have a page Aboutus - This has
I'm about to build my first Ruby on Rails application (and first anything beyond
I am working on the billing component of a Ruby on Rails application using
Imagine a web application written in Ruby on Rails. Part of the state of

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.