Setting default radio button on edit

Posted by DTown on Stack Overflow See other posts from Stack Overflow or by DTown
Published on 2010-04-20T16:15:27Z Indexed on 2010/04/21 0:53 UTC
Read the original article Hit count: 355

Filed under:

So I'm trying to setup scaffolding to use radio buttons for the format button. It definitely works to add a new and edit. The problem is when I go to edit an entry the correct radio button isn't selected by default.

<% form_for(@cinema) do |f| %>
<%= f.error_messages %>

<p>
  <%= f.label :title %><br />
  <%= f.text_field :title %>
</p>
<p>
  <%= f.label :director %><br />
  <%= f.text_field :director %>
</p>
<p>
  <%= f.label :release_date %><br />
  <%= f.date_select :release_date, :start_year => 1900, :end_year => 2010 %>
</p>
<p>
  <%= f.label :running_time %><br />
  <%= f.text_field :running_time %>
</p>
<p>Blockquote
  <%= f.label :format %><br />
  <%= f.radio_button :format, "black & white" %>
  <%= label :format_bw, "Black & White" %>
  <%= f.radio_button :format, "color" %>
  <%= label :format_color, "Color" %>
</p>
<p>
  <%= f.submit 'Create' %>
</p>

<% end %>

Controller

  def edit
    @cinema = Cinema.find(params[:id])
  end

Model

class Cinema < ActiveRecord::Base
validates_presence_of :title, :on => :create
validates_presence_of :title, :on => :update
#
validates_presence_of :director, :on => :create
validates_presence_of :director, :on => :update
#
validates_presence_of :release_date, :on => :create
validates_presence_of :release_date, :on => :update
#
validates_presence_of :format, :on => :create
validates_presence_of :format, :on => :update
#
validates_presence_of :running_time, :on => :create
validates_presence_of :running_time, :on => :update 
validates_numericality_of :running_time, :on => :create, :on => :update, :less_than_or_equal_to => 300, :greater_than => 0
 end

© Stack Overflow or respective owner

Related posts about ruby-on-rails