Topic: Help! Select Values/Labels not picked up in Edit record mode

I have a simple form for a servicerequest model (create / edit)When I create a a new SR, the proper VALUE is stored in the database just fine (value, not the display label), but when I try to retrieve it in the EDIT view, the select dropdown is NOT picking up the existing value/label and defaulting to the first one in the array.

(the priority is NOT a relationship to another model, all contained in a simple array for now).

What am I missing?

NEW & EDIT views (Both the same): 

  <p>
    <%= f.label :priority %><br />
    <%= f.select(:priority, [["Stat", "1"], ["Urgent", "2"], ["Regular", "3"], ["Safety", "4"]], { :prompt => "Choose"})  %>
  </p>

Last edited by yogi799 (2012-12-17 12:54:36)

SAVE MYSQL NOW, Go to: http://helpmysql.org

Re: Help! Select Values/Labels not picked up in Edit record mode

You could try using collection_select.

# File actionpack/lib/action_view/helpers/form_options_helper.rb, line 645
def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  @template.collection_select(@object_name, method, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options))
end

Here are two resources for you to check out:
http://shiningthrough.co.uk/Select-help … y-on-Rails
http://railscasts.com/episodes/88-dynamic-select-menus

Your code as I understand might go something like this:

Controller:
@priority = Priority.find(:all)

View:
<%= f.collection_select :priority_id, @priority, :id, :name, :prompt => "Select a Priority" %>

Mind you this is just my guess. Test it out. Modify it. I am not a Rails professional, nor did I even test it.

Last edited by tuscanidream (2012-12-17 22:33:50)

Learning rails, one day at a time.

http://keilmiller.com

Re: Help! Select Values/Labels not picked up in Edit record mode

Thanks tuscani... in fact, just 30 minutes ago, I got the help that I needed... Nothing worked before...Guess what... The only reason none of these iterations worked (even though I had a few of them right from the get-go) was... THE STRING VS THE INTEGER! The db stored 1s, 2s, and 3s as integers and we were comparing them to strings "1", "2", etc... When the line was changed to: {"Stat" => 1, "Urgent" => 2} it started working magically....

Last edited by yogi799 (2012-12-17 22:37:36)

SAVE MYSQL NOW, Go to: http://helpmysql.org

Re: Help! Select Values/Labels not picked up in Edit record mode

yogi799 wrote:

Thanks tuscani... in fact, just 30 minutes ago, I got the help that I needed... Nothing worked before...Guess what... The only reason none of these iterations worked (even though I had a few of them right from the get-go) was... THE STRING VS THE INTEGER! The db stored 1s, 2s, and 3s as integers and we were comparing them to strings "1", "2", etc... When the line was changed to: {"Stat" => 1, "Urgent" => 2} it started working magically....

Oh yeah! I can't believe I missed that.

Might want to store those values in a model instead of a hash. It would make it easier should you decide you want to dynamically change the names, or pull the names without copying a hash over and over. Not very dry. Just an idea. Glad you got it to work though!

Learning rails, one day at a time.

http://keilmiller.com

Re: Help! Select Values/Labels not picked up in Edit record mode

Yeah, I know. That's why I am not storing labels but integers - this is a pilot project for now so the quicker the better - but yes, the idea is to expand it in the future so the integers I am using for now will work.

SAVE MYSQL NOW, Go to: http://helpmysql.org

Re: Help! Select Values/Labels not picked up in Edit record mode

That integer vs string problem wasted me half of the day. I would have never guessed that I think. Tried everything to no avail.

SAVE MYSQL NOW, Go to: http://helpmysql.org