Mei
Mei

Reputation: 243

Haml Radio Button Pre-select Syntax

We use Ruby (1.9.2) Rails (2.3).

I'm trying to set pre-selection for radio buttons...

- form_for @user, :url => plan_user_url, :html => { :method => 'put', :class => 'form' }  do |f|
  - @plans.each do |p|
    %span
      %p= p[:blurb]
      %p= p[:price]
      - p[:features].each do |f|
        %p= f
      = f.radio_button {:id => p[:id], :checked => @user[:plan_id]==p[:id] || nil}
      = f.label :plan_name, p[:name]
    %p
      %br
  .spacer

  .field.first
    = f.submit 'Update', :class => 'button ok'
  .field
    = link_to 'Cancel', redirect_back_url || root_url, :class => 'button cancel'

HAML doesn't like this line:

  = f.radio_button {:id => p[:id], :checked => @user[:plan_id]==p[:id] || nil}

Any help is appreciated.

Upvotes: 2

Views: 9202

Answers (2)

Mei
Mei

Reputation: 243

Thanks for the hints from Brian. It turns out

f.radio_button :plan_id, p[:id]

works for pre-select.

Upvotes: -5

Brian Donovan
Brian Donovan

Reputation: 8390

This is invalid Ruby code:

= f.radio_button {:id => p[:id], :checked => @user[:plan_id]==p[:id] || nil}

You're attempting to call the radio_button method and Ruby thinks you're passing it a block, but really you're passing it a Hash. This is better:

= f.radio_button :id => p[:id], :checked => @user[:plan_id]==p[:id] || nil

That removes the ambiguity between Proc and Hash, but it's still weird. Why do you want the || nil? I think it's unnecessary:

= f.radio_button :id => p[:id], :checked => @user[:plan_id] == p[:id]

Upvotes: 11

Related Questions