开发者

Setting default radio button on edit

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>

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开发者_StackOverflow中文版_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


When you create a new record of whatever model you're rendering a form for, and don't give it a value for format, what will its value be? If it's nil, then that's your problem, since none of those checkboxes are "blank". You need to set up your database to give a default value to format that matches one of your radio button values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜