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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:35:23+00:00 2026-05-26T01:35:23+00:00

I don’t know which is the best or right way to make something, so

  • 0

I don’t know which is the best or right way to make something, so I’m looking for suggestions.

Stage:
I am using Rails 3 to make some web app.
Imaging two models: Canvas and Zone.
A canvas object intents to be a square in which there will be four zone‘s (north, south, east and west).
Each zone has these attributes: text:string and rgb_color:string.
I want to render a HTML form in which user must capture a Canvas registry, in other words, in this form will have four groups of zones fields.

Problem:
How can I capture all of them in one HTML form?

If I would have only one zone I can do something like this:

<%= form_for(@zone) do |f| %>
  <div class="field">
    <%= f.label :text %><br />
    <%= f.text_field :text %>
  </div>
  <div class="field">
    <%= f.label :rgb_color %><br />
    <%= f.text_field :rgb_color %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

But how should I do it with four zones?

  • 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-26T01:35:24+00:00Added an answer on May 26, 2026 at 1:35 am

    You can do this with accepts_nested_attributes_for and fields_for:

    Model:

    class Canvas < ActiveRecord::Base
      has_many :zones
      accepts_nested_attributes_for :zones
    end
    

    Controller:

    class CanvasesController < ApplicationController
      def new
        @canvas = Canvas.new
        4.times { @canvas.zones.build }
      end
    end
    

    View:

    <%= form_for(@canvas) do |f| %>
      <%= f.fields_for :zones do |zone| %>
        <div class="field">
          <%= zone.label :text %><br />
          <%= zone.text_field :text %>
        </div>
        <div class="field">
          <%= zone.label :rgb_color %><br />
          <%= zone.text_field :rgb_color %>
        </div>
      <% end %>
      <div class="actions">
        <%= f.submit %>
      </div>
    <% end %>
    

    For more details and explanation have a look at this:

    • Nested Model Form Part 1
    • Nested Model Form Part 2
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know how to google for such, but is there a way to query
Don't know if I worded the question right, but basically what I want to
Don't know a whole lot about streams. Why does the first version work using
Don't know if this is the right place to ask this, but I will
Don't know why this is happening, but after submitting a form via JS (using
I don't have much PHP experience and I want to know how to best
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
Don't know if there is a better way to do this, so that is
Don't you hate it when you have class Foobar { public: Something& getSomething(int index)
Don't really know how to formulate the title, but it should be pretty obvious

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.