简体   繁体   English

Rails上的date_select问题

[英]Problem with date_select on rails

I have following controller: 我有以下控制器:

  def personalization
    @title = t "generic.forms.personalization"
  end

  def update_personalization
    begin
      @user.user_data.birthdate = Date.civil(params[:user_data][:"birthdate(1i)"].to_i,params[:user_data][:"birthdate(2i)"].to_i,params[:user_data][:"birthdate(3i)"].to_i)
    rescue
      wrong_data = 1
    end
    if @user.user_data.update_attributes(params[:user_data])
      if wrong_data
        flash[:Error] = t "generic.messages.error.wrong_data"
        redirect_to :back and return
      end
      flash[:Success] = t "generic.messages.success.account_updated"
      redirect_to :back
    else
      flash[:Error] = @user.user_data.errors.full_messages.join(".<br>")
      redirect_to :back
    end
  end

and following view: 和以下视图:

<div id="ProfileEditForm" class="ContentBorders">

  <h1 class="FormsHeading"><%= t @title %></h1>
  <div class="FormsSpacing">

  <%= form_for(@user.user_data, :html => { :id => "UpdateUserForm", :class => "EditForms"}, :url => {:action => 'update_personalization'}) do |f| %>
  <% flash.each do |key, value| %>
    <div class="FormsMargins <%= key %>"><%=raw value + "." %></div>
  <% end %>

      <div class="Field"><div class="LabelInline"><%= t "generic.site.first_name" %>:</div>
      <%= f.text_field :first_name, :id => "EditFirstName", :class => "Rounded5", :maxlength => "30" %></div>

      <div class="Field"><div class="LabelInline"><%= t "generic.site.last_name" %>:</div>
      <%= f.text_field :last_name, :id => "EditLastName", :class => "Rounded5", :maxlength => "30" %></div>

      <div class="Field DateSelection"><div class="LabelInline"><%= t "generic.site.birthdate" %>:</div>
        <%= date_select("user_data", "birthdate", :start_year => 1901, :end_year => 2011, :include_blank => true) %>
      </div>

      <div class="Field GenderSelection"><div class="LabelInline"><%= t "generic.site.gender" %>:</div>
        <%= f.radio_button :gender, "0", :id => "EditGenderMale" %> <span><%= t "generic.site.male" %></span>
        <%= f.radio_button :gender, "1", :id => "EditGenderFemale" %> <span><%= t "generic.site.female" %></span>
      </div>

      <div class="Field EducationSelection"><div class="LabelInline"><%= t "generic.site.educational_level" %>:</div>
        <%= f.select :education_level, options_for_select({
          " " => 0, (t "generic.site.education_levels.first") => 1, (t "generic.site.education_levels.second") => 2,
          (t "generic.site.education_levels.third") => 3, (t "generic.site.education_levels.fourth") => 4,
          (t "generic.site.education_levels.fifth") => 5, (t "generic.site.education_levels.sixth") => 6,
          (t "generic.site.education_levels.seventh") => 7 }, @user.user_data.education_level) %>
      </div>

      <div class="Action"><%= f.submit (t "generic.forms.update_data"), :id => "EditSubmit", :class => "ProfileEditAction Shadow1 Rounded5 AcceptButtonsBorder" %></div>

  <% end %>

  </div>

</div>
<%= render :partial => 'profile_panel' %>

Now. 现在。 The problem is with date_select method. 问题出在date_select方法上。 Each of form field works properly (data from database fills them up), except that which was generated from data_select. 除了从data_select生成的表单字段外,每个表单字段均正常工作(数据库中的数据填满了它们)。

If I select some proper data, and click update button, then it saves that proper data to the db. 如果我选择一些适当的数据,然后单击“更新”按钮,则它将适当的数据保存到数据库中。 Problem comes with the moment, when it is generated, and it doesn't come with any values (it's always empty when loaded). 问题来自产生的那一刻,并且没有任何值(加载时始终为空)。

Any ideas, how can that be fixed? 任何想法,如何解决?

maybe... 也许...

you have: 你有:

<%= date_select("user_data", "birthdate", :start_year => 1901, :end_year => 2011, :include_blank => true) %>

you need: 你需要:

<%= f.date_select("user_data", "birthdate", :start_year => 1901, :end_year => 2011, :include_blank => true) %>

just put the "f" 只需把“ f”

<%= f.date_select ... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM