Topic: Select input defaults to NIL when using Edit

I have a form with a drop down box that allows a user to select predefined input instead of typing in new data....everything works fine here and the users input gets saved to the database. The problem comes when the user wants to change their prior input and goes to the "Edit" page. All of the user's previous answers are there except for their previous selection with the drop down box, that has been reset to the top choice. I would like to have the select menu default to the users previously stored input instead of changing everytime to the top choice in the menu.

Here is the code I'm using. For instance, if a user previously selected Option 2 from this drop down and goes in to try and edit it, his previous choice is lost and the Edit form shows
the first choice in the list (option value="") instead of what is stored in the database for the users previous Catagory choice.

<label for="link_graph">Catagory</label>
<select name ="link[catagory]">
<option value="">
<option value="Option 1">Option 1
<option value="Option 2">Option 2
<option value="Option 3">Option 3
</select>

Any help or guidance would be greatly appreciated!

jackster

Re: Select input defaults to NIL when using Edit

You can use a rails helper for this:

<%= select :link, :category, ['Option 1', 'Option 2', 'Option 3'] %>

This should automatically select the value in "@link.category", so set the @link instance variable in the controller if you aren't doing so.

Railscasts - Free Ruby on Rails Screencasts

Re: Select input defaults to NIL when using Edit

YES! That worked PERFECTLY! Exactly as I had hoped.

Thanks so much for the help on this one, RyanB!

jackster

Re: Select input defaults to NIL when using Edit

I am having a similar problem with the automatic selection value. I am using two separate models and need the values from one (Headings) in a drop down menu to be set the another (Press). Both models are controlled by the same controller. It works fine except selecting a previously set value in the drop down.

Inside a edit method I set an iv called @press = Press.find(params[:id]) and it gets passed to the edit.rhtml used by a scaffold generated _form partial. 

Here is the select helper.

<p><label for="press_heading">Heading</label><br/>
<%=
    # set up a conditional to display a blank option or the current heading
    if @press.heading
        select 'press', 'heading', PressHeading.find_all.collect {|h| [h.heading, h.id]}
    else
        select 'press', 'heading', PressHeading.find_all.collect {|h| [h.heading, h.id]}, {:include_blank => true}
    end
    %></p>

The conditional works great but the previously selected value doesn't get selected.

I tried to use the {:selected => "#{@press.heading}"} option and pass the value which I know exists (and is a numerical value) but it doesn't evaluate correctly (I'm new to rails and ruby).  If I manually insert a numerical value for the selected option it works.

Any advise please,
phil

Last edited by philmill (2007-02-01 13:54:03)

Re: Select input defaults to NIL when using Edit

You probably want heading_id instead of "heading" for the select menu:

if @press.heading
  select 'press', 'heading_id', PressHeading.find_all.collect {|h| [h.heading, h.id]}
else
  select 'press', 'heading_id', PressHeading.find_all.collect {|h| [h.heading, h.id]}, {:include_blank => true}
end

BTW, here's a better way to do this:

<%= collection_select :press, :heading_id, PressHeading.find(:all), :id, :heading, :include_blank => @press.heading.nil? %>

Untested.

Railscasts - Free Ruby on Rails Screencasts

Re: Select input defaults to NIL when using Edit

Thanks for the better way but I still can't select the right one.  The heading_id method is not defined and I am not sure how to define with the correct info. 

PressHeading is a class that holds names of email headings and Press is another class that contains the entire email data.  @press.heading (from @press = Press.find(params[:id]) ) returns the id number from PressHeading and not the name stored in that id cell.

I use that number to find the correct name in the PressHeading table for displaying.  Is there a better way to set up this database structure or just select the right one?

Re: Select input defaults to NIL when using Edit

Oh, is "heading" a custom method you made for fetching the id of the proper heading? You'll need a setter method (heading=) as well to get this form working. If you post the Press model I can post the code you'll need.

Railscasts - Free Ruby on Rails Screencasts

Re: Select input defaults to NIL when using Edit

Sorry about the confusion but I am new and trying to use correct Ruby language.  In @press.heading from my controller call @press = Press.find(params[:id]), I thought .heading was fetching the row named "heading" with the id from

<% @presses.each do |p| %>
<%= link_to p.title, {:controller => 'press', :action => 'edit', :id => p.id}, :method => :post %>.
<% end %> where @presses = Press.find(:all)

It seems to be true if I display "#{@press.heading}" in the rhtml.
So no "heading" is not a custom method I am just using it incorrectly, I think.

Re: Select input defaults to NIL when using Edit

Okay, so it is a column in the database? Is the column of type "string"? That would be the problem then. Ruby is comparing string to integer and thinks it doesn't match. I recommend changing the type of the column. Or you can override the getter method to return an integer:

# in Press
def heading
  self[:heading].to_i
end

But, if you want to go with the Rails conventions (which I highly recommend), you would need to rename the "heading" column to "press_heading_id" and make it of type "integer" then you can do this:

class Press < ActiveRecord::Base
  belongs_to :press_heading
end

class PressHeading < ActiveRecord::Base
  has_many :presses
end


In that case you would pass press_heading_id to the collection_select.

<%= collection_select :press, :press_heading_id, PressHeading.find(:all), :id, :heading, :include_blank => @press.heading.nil? %>

There are many other benefits to setting up the relationship like this.

Railscasts - Free Ruby on Rails Screencasts

Re: Select input defaults to NIL when using Edit

WOW thanks Ryan for all quick responses and directed help.  I followed your advice and changed the type using migrations and then added the class declarations.  Class-level declarations are  green to me:-)

Last edited by philmill (2007-02-01 16:49:20)

Re: Select input defaults to NIL when using Edit

Now I am having trouble displaying the actual name of the heading instead of the id in the database. 
I am trying to list all the press titles and headings as follows

<% @presses.each do |p| %>
<%= "#{p.date} " %>
<%= link_to p.title, {:controller => 'press', :action => 'edit', :id => p.id}, :method => :post %>
<%= "INSERT CORRECT CALL TO PressHeading" %>
<%= link_to ' [X]', {:action => 'destroy', :id => p.id}, :confirm => 'Delete Release?', :method => :post %><br><br>
<% end %>

where @presses = Press.find(:all)

I have tried to set @headings = PressHeading.find(:all) and
somehow address @headings with p.press_heading_id in the view with no luck.

This seems simple and I'm about to loose it.

Re: Select input defaults to NIL when using Edit

This is where that association I showed you how to set up in the previous post comes into play.

<% for press in @presses %>
  ...
  <%= press.press_heading.heading %>
  ...
<% end %>

Railscasts - Free Ruby on Rails Screencasts

Re: Select input defaults to NIL when using Edit

That absolutely worked.

I need to hit the books hard.  This is more of a Ruby issue for me.  Do you have any suggestions for a site to get my learn on.

Re: Select input defaults to NIL when using Edit

Agile Web Development with Rails is pretty much the book to read on Rails. This will show you how to do all of this Active Record model association stuff - and a whole lot more.

If you're looking for something free on the web, there's always the official documentation but that isn't the best source for Rails beginners.

Railscasts - Free Ruby on Rails Screencasts