【问题标题】:uninitialized constant ... error未初始化的常量...错误
【发布时间】:2011-06-18 01:02:24
【问题描述】:

我收到以下错误:uninitialized constant Profile::STRING_LEN_MAX_DB

这是我所拥有的:

application_helper.rb

def field ( form, field_name, len = TEXT_FIELD_LEN, max_len = STRING_LEN_MAX_DB )
    x = content_tag "label", "#{field_name.humanize}:", :for => field_name
    y = form.text_field field_name, :size => len, :maxlength => max_len { "#{x}  #{y} <br />"}
end

编辑视图

<% form_for :profileinfo do |fe| %>
<fieldset>
<info><%= @titlu %></info>
<%= error_messages_for 'profileinfo' %>
<div class="fields">
    <%= field fe, "first_name" %>
    <%= field fe, "last_name" %>

    <label for="gender">Sex:</label>
    <%= radio_button :profileinfo, :gender, "Male" %> Male
    <%= radio_button :profileinfo, :gender, "Female" %> Female
    <br />
    <label for="birth_date">Date of birth:</label>
    <%= date_select :profileinfo, :birth_date, :start_year => Profile::YEAR_FT, :end_year => Time.now.year, :include_blank => true, :order => [:month, :day, :year] %>
    <br />

    <%= field fe, "ocupation" %>
    <%= field fe, "city" %>
    <%= field fe, "country" %>
    <%= field fe, "postal_code", Profile::PSTCOD_LEN %>
    <%= submit_tag "Save", :class => "submit" %>
</div>
</fieldset>
<% end %>

config\environments.rb

# Load the rails application
require File.expand_path('../application', __FILE__)

# Be sure to restart your server when you modify this file
# Initialize the rails application
IRC07::Application.initialize! do |config|
STRING_LEN_MAX_DB=255
TEXT_FIELD_LEN=15
end

我在environment.rb 中进行更改后重新启动了服务器,但我仍然收到同样的错误。

【问题讨论】:

    标签: ruby-on-rails constants initialization


    【解决方案1】:

    移动这些行:

    STRING_LEN_MAX_DB=255
    TEXT_FIELD_LEN=15
    

    退出配置循环。

    所以你的 enviroment.rb 看起来像:

    # Load the rails application
    require File.expand_path('../application', __FILE__)
    
    # Be sure to restart your server when you modify this file
    # Initialize the rails application
    IRC07::Application.initialize! do |config|
    end
    STRING_LEN_MAX_DB=255
    TEXT_FIELD_LEN=15
    

    如果这不起作用,请尝试: # 加载rails应用 需要 File.expand_path('../application', FILE)

    # Be sure to restart your server when you modify this file
    # Initialize the rails application
    IRC07::Application.initialize! do
      STRING_LEN_MAX_DB=255
      TEXT_FIELD_LEN=15
    end
    

    【讨论】:

      【解决方案2】:

      我更改了以下代码:app\views\editprofile\edit.html.erb

      <%= form_for :profileinfo do |fe| %>
      <fieldset>
      <info><%= @titlu %></info>
      <%= error_messages_for 'profileinfo' %>
      <div class="fields">
      <%= text_field_for fe, "first_name" %>
      <%= text_field_for fe, "last_name" %>
      <label for="gender">Gender:</label>
      <%= radio_button :profileinfo, :gender, "Male" %> Male
      <%= radio_button :profileinfo, :gender, "Female" %> Female
      <br />
      <label for="birth_date">Birthdate:</label>
      <%= date_select :profileinfo, :birth_date, :start_year => Profile::YEAR_FT, :end_year => Time.now.year, :include_blank => true, :order => [:month, :day, :year] %>
      <%= text_field_for fe, "ocupation" %>
      <%= text_field_for fe, "city" %>
      <%= text_field_for fe, "country" %>
      <%= text_field_for fe, "postal_code", Profile::PSTCOD_LEN %>
      <%= submit_tag "Update", :class => "submit" %>
      </div>
      </fieldset>
      <% end %>
      

      还有 application_helper 中的函数 text_field_for

         def text_field_for(form, field, size=TEXT_FIELD_LEN, maxlength=STRING_LEN_MAX_DB)
          label = content_tag("label", "#{field.humanize}:", :for => field)
          form_field = form.text_field field, :size => size, :maxlength => maxlength
          content_tag("div", "#{label} #{form_field}", :class => "fields")
         end  
      

      性别和生日都可以,但是对于文本字段,我没有文本字段,而是得到带有所有参数的 html 标签......像这样:

          <label for="first_name">First name:</label> <input id="profileinfo_first_name" maxlength="255" name="profileinfo[first_name]" size="15" type="text" value="" />
          <label for="last_name">Last name:</label> <input id="profileinfo_last_name" maxlength="255" name="profileinfo[last_name]" size="15" type="text" value="" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-08
        • 2011-09-29
        • 2017-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        相关资源
        最近更新 更多