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

The Archive Base Latest Questions

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

I’m having a hard time wrapping my head around a collection_select that I’m trying

  • 0

I’m having a hard time wrapping my head around a collection_select that I’m trying to do. Some background: I have a nested model where I have an layout and each layout can have a set number of images. As part of the input, the user is allowed to decide what image goes in what order. This is dictated by the layout_placement value of the image. This currently works, the layout_placement is a plain text field where the user can input etc.

<%= f.fields_for :images do |builder| %>
  <%= builder.file_field :image %> 
  <%= builder.label :layout_placement %> <%= builder.text_field :layout_placement, :size => 3%>
  <br /><br />
<% end %>

What I need though is a drop-down in place of the plain text field that will allow me to limit the value of the layout_placement selected – this is based on the number of spaces available (each layout is different and has anywhere from 2 to 5 image slots).

I already have a field in the layout called image_slots that gives me the number of available image slots for the layout being used. So I want to make my available options a range from 1 to the number of image slots. I’m trying to do something like this:

<%= builder.collection_select :layout_placement, 1..@layout.image_slots, ???, ??? %>

And I get stuck on what to pass for the :id and :name methods. I’ve tried going simpler and just using the select_tag but I get stumped in a different area in that, since it’s a nested model the :object#method settings goes wrong.

Any help in making either option work would 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-25T11:59:18+00:00Added an answer on May 25, 2026 at 11:59 am

    In your case you don’t really need to use the collection_select since you want the text and value of your options to be the same thing (ie: 1..@layout.image_slots). Try this

    <%= builder.select :layout_placement, 1..@layout.image_slots %>
    

    In the case where your image_slots might return nil just make sure you handle it properly

    <%= builder.select :layout_placement, 1..(@layout.image_slots || 1) %>
    

    To validate the placement values are uniq, add this to your Image model

    validates_uniqueness_of :layout_placement, :scope => :layout_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.