Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to dynamically change position of form based on selection
    primarykey
    data
    text
    <p>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)</p> <p>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. </p> <p>Each subcategory has a position field, which dictates where it is displayed under the category.</p> <p>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. </p> <p>My current idea to track the available position for subcategory in my Application Controller is:</p> <pre><code>def subcategory_count(subcategory) category = subcategory.category @count = category.subcategories.count end </code></pre> <p>I then add +1 to <code>@count</code> so it gives me the available positions, +1. (If anyone has a better way of doing this, please let me know.)</p> <p>I am currently following Ryan Bates' railscasts 88 revised which is dynamic select menus revised. Here is what my form looks like right now.</p> <pre><code>&lt;%= form_for(@subcategory) do |f| %&gt; &lt;p&gt; &lt;%= f.label(:name) %&gt; &lt;%= f.text_field :name %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label(:category_id) %&gt; &lt;%= f.collection_select :category_id, Category.order(:position), :id, :name, include_blank: true %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :position, "Position" %&gt; &lt;%= f.grouped_collection_select :position, Category.order(:position), :subcategories, :name, :position, :position%&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.submit("Submit") %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload