What is the best way to create a drop down where there is no backend model to get the values from? I am currently using: <%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p, p ]}).reverse, {:prompt=>"Select a Year"}) %> but this gives me issues when I am trying to edit the info as the currently saved value from the database is lost when edit action is called. -- Posted via http://www.ruby-forum.com/.
2009/8/31 Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > What is the best way to create a drop down where there is no backend > model to get the values from? > > I am currently using: > > <%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p, p > ]}).reverse, {:prompt=>"Select a Year"}) %> > > but this gives me issues when I am trying to edit the info as the > currently saved value from the database is lost when edit action is > called. > --You said at the top that there is no model to get the values from, so how can there be a saved value in the db? Colin
Man i always post my questions wrong, My dropdown will contain date range from 1980 to current year where as the db will only contain the saved years say 2000. -- Posted via http://www.ruby-forum.com/.
2009/8/31 Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Man i always post my questions wrong, > > My dropdown will contain date range from 1980 to current year where as > the db will only contain the saved years say 2000.Assuming that you have form_for @some_object do |f| where @some_object is the object being edited and that @some_object.year_made contains the previous selection then the dropdown should default to the current selection, I think. Colin
Well with this code, <%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p, p ]}).reverse, {:prompt=>"Select a Year"}) %> it does not seem to work and that makes me wonder what is wrong with this code? -- Posted via http://www.ruby-forum.com/.
2009/8/31 Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Well with this code, > > > <%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p, p > ]}).reverse, {:prompt=>"Select a Year"}) %> > > it does not seem to work and that makes me wonder what is wrong with > this code?What does the form_for line look like? What does generated html of the select look like? (View, Page Source or similar in your browser) Have you checked that year_made contains the previous value. Possibly put <%= @object.year_made %> in the form to check. Colin
> What does the form_for line look like?<% form_for @vehicle do |f| %>> What does generated html of the select look like? (View, Page Source > or similar in your browser)For New http://pastie.org/600224 For Edit http://pastie.org/600226> Have you checked that year_made contains the previous value. Possibly > put <%= @object.year_made %> in the form to check.Yes it does. -- Posted via http://www.ruby-forum.com/.
2009/8/31 Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> >> What does the form_for line look like? > <% form_for @vehicle do |f| %> > >> What does generated html of the select look like? (View, Page Source >> or similar in your browser) > > For New > http://pastie.org/600224 > > For Edit > http://pastie.org/600226 > > >> Have you checked that year_made contains the previous value. Possibly >> put <%= @object.year_made %> in the form to check. > Yes it does.Odd, it all looks ok to me. Is year_made an integer or a string? I wonder whether it is expecting an integer as all my uses have always been with an id value. Any other ideas anyone? Colin
Well it turns out that it was a string vs integer issue. I have my fields in db as string and the code was generating a integer so that caused issues at edit time. I made the following change and now it seems to work. <%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p.to_s, p.to_s ]}).reverse, {:prompt=>"Select a Year"}) %> Does anyone else have a better idea on how to do this? --------------------------- Colin Law wrote:> 2009/8/31 Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >> For Edit >> http://pastie.org/600226 >> >> >>> Have you checked that year_made contains the previous value. Possibly >>> put <%= @object.year_made %> in the form to check. >> Yes it does. > > Odd, it all looks ok to me. Is year_made an integer or a string? I > wonder whether it is expecting an integer as all my uses have always > been with an id value. > > Any other ideas anyone? > > Colin-- Posted via http://www.ruby-forum.com/.
No. This is the right way to do it. Data type matches are necessary for functionality, you can''t ignore those. On Sep 2, 3:45 pm, Quee Mm <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Well it turns out that it was a string vs integer issue. I have my > fields in db as string and the code was generating a integer so that > caused issues at edit time. > > I made the following change and now it seems to work. > > <%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p.to_s, > p.to_s ]}).reverse, {:prompt=>"Select a Year"}) %> > > Does anyone else have a better idea on how to do this? > > --------------------------- > > > > Colin Law wrote: > > 2009/8/31 Quee Mm <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: > >> For Edit > >>http://pastie.org/600226 > > >>> Have you checked that year_made contains the previous value. Possibly > >>> put <%= @object.year_made %> in the form to check. > >> Yes it does. > > > Odd, it all looks ok to me. Is year_made an integer or a string? I > > wonder whether it is expecting an integer as all my uses have always > > been with an id value. > > > Any other ideas anyone? > > > Colin > > -- > Posted viahttp://www.ruby-forum.com/.
2009/9/2 Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Well it turns out that it was a string vs integer issue. I have my > fields in db as string and the code was generating a integer so that > caused issues at edit time. > > I made the following change and now it seems to work. > > <%= f.select(:year_made, ((1980..Time.now.year).collect {|p| [ p.to_s, > p.to_s ]}).reverse, {:prompt=>"Select a Year"}) %> > > Does anyone else have a better idea on how to do this?You probably only need the to_s on one of them not both. Presumably it would also work if, instead, the type of year_made were changed to integer. Arguably this might be a more aesthetically pleasing solution. Colin> > --------------------------- > > Colin Law wrote: >> 2009/8/31 Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>: >>> For Edit >>> http://pastie.org/600226 >>> >>> >>>> Have you checked that year_made contains the previous value. Possibly >>>> put <%= @object.year_made %> in the form to check. >>> Yes it does. >> >> Odd, it all looks ok to me. Is year_made an integer or a string? I >> wonder whether it is expecting an integer as all my uses have always >> been with an id value. >> >> Any other ideas anyone? >> >> Colin > > -- > Posted via http://www.ruby-forum.com/. > > > >
I agree with you on that. And will think about converting to integer, I do have two more drop downs with similar values but they do have one or more string values so they do require to be strings but the year_made does not. -- Posted via http://www.ruby-forum.com/.
Back again with another question. For the drop down below <%= f.select(:year_imported, ((1980..Time.now.year).collect {|p| [ p.to_s, p.to_s ]}).reverse, :prompt=>"Select a Year") %> I also want this to contain a value, "Local" as the first one in the list. What is the best way to do this? Quee Mm wrote:> I agree with you on that. And will think about converting to integer, I > do have two more drop downs with similar values but they do have one or > more string values so they do require to be strings but the year_made > does not.-- Posted via http://www.ruby-forum.com/.
Hi, What essentially you are doing here is creating an array. So you can prepend "Local" in the array like this ((1980..Time.now.year).collect {|p| [p.to_s, p.to_s ]}).reverse.unshift "Local" I will also suggest you move out this array creation, and put it as a helper method. -- अभिनव http://twitter.com/abhinav On Thu, Sep 3, 2009 at 1:09 PM, Quee Mm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > Back again with another question. > > For the drop down below > > <%= f.select(:year_imported, ((1980..Time.now.year).collect {|p| [ > p.to_s, p.to_s ]}).reverse, :prompt=>"Select a Year") %> > > I also want this to contain a value, "Local" as the first one in the > list. What is the best way to do this? > > Quee Mm wrote: > > I agree with you on that. And will think about converting to integer, I > > do have two more drop downs with similar values but they do have one or > > more string values so they do require to be strings but the year_made > > does not. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---