Rails设计fields_for不工作

时间:2014-02-13 20:18:27

标签: ruby-on-rails view devise formula fields-for

我正在使用Devise,但我不认为这在这里非常重要。

我根本看不到创建的fields_for。

这些是我的文件:

new.html.erb

<%= form_for(resource, :as => resource_name, html: { id: 'registration_form', class: '' }, :url => registration_path(resource_name)) do |f| %>

  <!-- Error messages -->
  <%= devise_error_messages! %>

    <%= render 'new_user_fields', f: f %>

    <!-- Fields for user-information -->
    <%= f.fields_for :user_info do |f| %>

      <%= render 'shared/user_info_fields_for_sign_up', f: f, resource: resource %>

    <% end %>

    <!-- Fields for driver-information -->
    <%= f.fields_for :driver do |f| %>

      <%= render 'shared/driver_fields_for_sign_up', f: f, resource: resource %>

    <% end unless resource.driver.nil? %>

  <!-- Submit button -->
  <%= f.submit "Sign up", class: "" %>

<% end %>

共享/ _user_info_fields_for_sign_up.html.erb

<%= f.label t :label_gender %>
<%= f.collection_select :gender_id, Gender.all, :id, :type, class: '' %>

<%= f.label t :label_first_name %>
<%= f.text_field :first_name, class: "" %>

<%= f.label t :label_last_name %>
<%= f.text_field :last_name, class: "" %>

<%= f.label t :label_year_of_birth %>
<%= f.text_field :year_of_birth, class: "" %>

<%= f.label t :label_country %>
<%= f.select :country_alpha2, options_for_select: { Country.all }, class: '' %>

<%= f.label t :label_city %>
<%= f.text_field :city, class: "" %>

<%= f.fields_for :languageskills do |f| %>

  <%= render 'shared/languageskill_fields', f: f %>

<% end unless resource.user_info.languageskills.empty?%>

<%= link_to_add_fields t :label_add_language, f, :languageskills %>

<%= f.label t :label_interests %>
<%= f.text_field :interests, class: "" %>

<%= f.label t :label_about %>
<%= f.text_field :about, class: "" %>

RegistratiosController.rb

def new
    super
    resource.build_user_info
    resource.user_info.languageskills.build
    if params[:is_driver].to_i == 1
      resource.build_driver
    end
    Rails.logger.debug(resource.build_user_info.inspect)
end

CONSOLE.LOG

Started GET "/en/sign_up?is_driver=1" for 127.0.0.1 at 2014-02-13 13:20:01 +0100
Processing by RegistrationsController#new as JS
  Parameters: {"is_driver"=>"1", "locale"=>"en"}
  Rendered registrations/_new_user_fields.html.erb (12.1ms)
  Rendered registrations/new.html.erb within layouts/application (27.8ms)
  Rendered layouts/_header.html.erb (2.8ms)
  Rendered layouts/_messages.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.6ms)
   (0.2ms)  begin transaction
   (0.1ms)  commit transaction
#<UserInfo id: nil, user_id: nil, first_name: nil, last_name: nil, year_of_birth: nil, city: nil, created_at: nil, updated_at: nil, gender_id: nil, interests: nil, about: nil, country_alpha2: nil>
Completed 200 OK in 117ms (Views: 96.5ms | ActiveRecord: 0.4ms)

正在加载_new_user_fields.html.erb而没有任何问题。 我在application_controller.rb中将所有可能的符号插入到设计的输入参数中。虽然这没关系,对吧?

你能指出我正确的方向吗?我没有看到我的错:S

1 个答案:

答案 0 :(得分:0)

刚刚找到了bug的来源,并将为它打开一个新帖子。

我无法打电话

resource.build_nested_model

在我的控制器中。但是当我在视图中调用它时,它可以工作。当嵌套模型传递给视图时,它似乎没有保存到资源中。

链接到新主题

Rails Devise Build nested model in controller