Jump to content

The ultimate community for Ruby on Rails developers.


Photo

What's the sex type on the scaffold create step?

type scaffold ruby

  • Please log in to reply
1 reply to this topic

#1 RumblefishX

RumblefishX

    Passenger

  • Members
  • 1 posts

Posted 03 February 2014 - 12:38 PM

Hi guys!

 

I'm new in the ruby community and i'm learning about that yet.

 

When i try to set a sex type on the scaffold create step i dont know which type can i use. The Rails put components according database type, so... i need to show two radio objects but which column type can i use to do that?

 

Another question...

 

i create a datetime atribute to user entity through scaffold, but the years that appears on the combobox has begun on 2001 to 2014. How can i make this combo show more years before 2001?



#2 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 04 February 2014 - 01:03 PM

Wher you want a multiple choice rather than just a boolean then use a string for the gender field in the database and map an array of strings to the value that specify the gender text you wish to show the user.

 

e.g. for your view you might want something like this

<div class="field">
  <%= f.label :gender %><br>
  <%= f.select :gender, SomeClass::GENDER_TYPES,
      prompt: 'Select a payment method' %>
</div>

In SomeClass (Whatever class probably the class you are trying to scaffold) define the constant GENDER_TYPES array and add a validation to ensure that only those values defined in the array can be saved to the database

 

Something like this

class SomeClass < ActiveRecord ...
    GENDER_TYPES = [ "Male", "Female", "Do not wish to say" ]

    validates :gender, inclusion: GENDER_TYPES
end

Obviously change SomeClass in the view to be the actual name of the class you define the array and the validation in

 

That's really all there is too it


Programming is just about problem solving!






Also tagged with one or more of these keywords: type, scaffold, ruby

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users