2011-08-21 2 views
1

레일즈 3.1.0.rc6을 러닝 할 때 모델을 사용하여 드롭 다운 (요소 선택)을 이해하는 데 어려움을 겪고 있습니다.모델의 레일즈 3.x 드롭 다운

<%= form_for @user do |f| %> 
     <div class="input"> 
     <%= f.label :email %> 
     <%= f.text_field :email %> 
     </div> 

     <div class="input"> 
     <%= f.label :password %> 
     <%= f.password_field :password %> 
     </div> 

     <div class="input"> 
     <%= f.label :password_confirmation, "Repeat Password" %> 
     <%= f.password_field :password_confirmation %> 
     </div> 

     <div class="input"> 
     <%= f.label :first_name %> 
     <%= f.password_field :first_name %> 
     </div> 

     <div class="input"> 
     <%= f.label :last_name %> 
     <%= f.password_field :last_name %> 
     </div> 

     <div class="input"> 
     <%= f.label :birthday %> 
     <%= date_select "user", "birthday" %> 
     </div> 

     <div class="input"> 
     <%= f.label :gender %> 
     <%= select_tag :gender, options_for_select([['Male', 'male'], ['Female', 'female']]) %> 
     </div> 

     <div class="submit"> 
     <%= f.submit nil, :class => ['button', 'button_blue'] %> 
     </div> 
    <% end %> 

지금 나는 생일이 선택과 성 선택 사용하기 쉬운을하는 방법을 이해하지 않습니다

나는 다음 있습니다. 생성 된 HTML은 모델 저장과 함께 사용하기가 쉽지 않습니다.

도움이 될 것입니다. 감사.

목표 : 게시 된 데이터로 쉽게 User.create를 수행 할 수 있습니다.

답변

1

당신이 선택의 이름과 ID가 당신에게 의지를 만드는 방식으로 생성되어 있는지 확인합니다 직접적

f.select(:gender, options_for_select([["Male", "male"],["Female","female"]])) 

같은 'F'변수 끄기를 선택 폼 빌더 도우미를 사용할 수 있습니다 자동으로 집어들.

+0

달콤한, 고마워요! –

1

(formtastic을 기반으로) simple_form을 강력히 사용하는 것이 좋습니다. 수만 명의 개발자가 매일 45 개의 포크 등을 사용하며 대부분의 조직에서 표준으로 사용됩니다. 당신은 결코 그렇게 간단하지는 않지만 유연한 것을 되돌아 보지 않을 것입니다. 호세 발림 (Jose Valim)이 저서에 저서를 저술 한이 책은 지역 사회의 지도자 중 한 사람입니다.

https://github.com/plataformatec/simple_form 

작은 발췌 (모델 드롭 다운에 대한 특정 질문 (요소 선택)을 해결하기 : -

...

Now we have the user form: 
<%= simple_form_for @user do |f| %> 
    <%= f.input :name %> 
    <%= f.association :company %> 
    <%= f.association :roles %> 
    <%= f.button :submit %> 
<% end %> 

Simple enough right? This is going to render a :select input for choosing the :company, and another :select input with :multiple option for the :roles. You can of course change it, to use radios and check boxes as well: 
f.association :company, :as => :radio 
f.association :roles, :as => :check_boxes 

...

당신은 아직 할 수 좋은 캘린더 팝업 날짜 선택 도구 인 date_calendar gem을 사용하면 Does anyone know any gems/plugins/tutorials related to exporting events to iCal, Google Calendar, Outlook from a Rails application?도 도움이 될 수 있습니다.

+0

감사합니다. 저것 좀 봐주세요! 지금 당장은 필자의 솔루션으로 f.select를 사용할 것이다. –

0

날짜를 선택하려면 jquery datepicker를 사용하면 매우 편리합니다. 모든

첫째, 당신이 자동으로 ID = 'user_birthday'을 주어집니다이

<div class="field"> 
    <%= f.label :birthday %><br /> 
    <%= f.text_field :birthday %> 
</div> 

처럼 양식 필드를 작성하는 경우 (NB : 텍스트 필드로 설정하는데주의를 기울여야). 다음 jquery를 사용하여 자바 스크립트에 간단한 구성을 추가하십시오.

$(document).ready(function() { 

    $.datepicker.setDefaults({ 
     dateFormat: 'dd/mm/yy', 
     firstDay: 1, 
     showOn: 'focus' 
    }); 

    $('#user_birthday').datepicker(); 

}); 
+0

이것은 좋지만 사람들은 생일과 같은 것을 위해 드롭 다운을 사용하는 것에 익숙합니다. 사람들은 이렇게 피커를 사용하기에는 너무 멀었습니다. –