rails上的date_select问题

时间:2011-08-02 08:21:45

标签: ruby-on-rails ruby-on-rails-3

我有以下控制器:

  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

以下观点:

<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' %>

现在。问题在于date_select方法。每个表单字段都正常工作(来自数据库的数据填充它们),但是从data_select生成的除外。

如果我选择了一些正确的数据,然后单击“更新”按钮,则会将正确的数据保存到数据库中。当问题出现时,问题就出现了,它没有任何值(加载时它总是空的)。

任何想法,如何解决?

1 个答案:

答案 0 :(得分:1)

也许...

你有:

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

你需要:

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

只需输入“f”

<%= f.date_select ...