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

The Archive Base Latest Questions

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

I am building a website that is basically a small Content Management System. (in

  • 0

I am building a website that is basically a small Content Management System. (in the sense that the user will be able to dynamicaly change most content of the site)

In my rails app I have two models, a Category and a Subcategory. A Category has many Subcategories, and a Subcategory belongs to a Category.

Each subcategory has a position field, which dictates where it is displayed under the category.

When creating a subcategory by form, I want the user to have the option to select the category from a list of all categories in the database (Which I have working fine). I then want the position field (a dropdown box) to update based on what positions are currently available within that category.

My current idea to track the available position for subcategory in my Application Controller is:

def subcategory_count(subcategory)
category = subcategory.category
@count = category.subcategories.count
end 

I then add +1 to @count so it gives me the available positions, +1. (If anyone has a better way of doing this, please let me know.)

I am currently following Ryan Bates’ railscasts 88 revised which is dynamic select menus revised. Here is what my form looks like right now.

<%= form_for(@subcategory) do |f| %>
<p>
<%= f.label(:name) %>
<%= f.text_field :name %>
</p>
<p>

<%= f.label(:category_id) %>  
<%= f.collection_select :category_id, Category.order(:position), :id, :name,     include_blank: true %>

</p>

<p>
<%= f.label :position, "Position" %>
<%= f.grouped_collection_select :position, Category.order(:position), :subcategories,  :name, :position, :position%>
</p>
<p>


<%= f.submit("Submit") %>
</p>
<% end %>

The problem with this code is that I need it to display as an int so I can add +1 to it, otherwise when there are no subcategories in the database it just shows blank and doesn’t allow me to pick the “1” position.

So, I need to find a way to update the field to show the available positions in the category, but I don’t know how to do that exactly. Any help will be greatly appreciated.

  • 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-29T20:26:35+00:00Added an answer on May 29, 2026 at 8:26 pm

    There are different solutions to your problem.

    If continue use your AJAX-less solution, try to override or just create different method on Category to return existing subcategories plus one new (for stub purpose only):

    class Category
    has_many :subcategories
    
    def subcategories_with_placeholder
      result = [Subcategory.new :position => (subcategories.length + 1)]
      subcategories.each{|s| result << s}
      result
    end
    #other class code
    end
    

    And replace

    <%= f.grouped_collection_select :position, 
      Category.order(:position), 
      :subcategories,
      :name, 
      :position, 
      :position%>
    

    With:

    <%= f.grouped_collection_select :position, 
      Category.order(:position), 
      :subcategories_with_placeholder,  
      :name, 
      :position, 
      :position%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a website that will be an open-source, user-contributed content kind of thing,
I'm building a website that will require user registration and logon. I would like
I'm building a website that uses xsl stylesheets, and I'm building up a small
I'm building a website that allows user to connect using Facebook Connect. So far
I am currently building a website in codeigniter that is one page site, basically
I am building a website where I have a need that user should be
I am currently building a small website where the content of the main div
am building a website that will be in multi language, I was wondering how
I am building a website that will have hundreds of pages. Each of these
I'm building a website that will authenticate users via Facebook connect and I'm torn

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.