Как отображать данные в форме Rails для вложенных атрибутов - Mongoid

У меня есть такая модель:

class User

    include Mongoid::Document
    include Mongoid::Timestamps
    include ActiveModel::SecurePassword

    ...
    field :import_settings,     :type => Hash,          default: lambda { default_values }

    def default_values
        {
            hosts:      '',
            port:       '',
            database:   '',
            user:       '',
            password:   ''
        }
    end

end

И моя форма выглядит так:

<%= form_for(@user, {:url => admin_path, :controller => 'stactic_pages', :action => 'update_import'}) do |f| %>

      <%= render 'shared/error_messages' %>

      ...

      <%= f.fields_for :import_settings do |p| %>

          <%= p.label :hosts %>
          <%= p.text_field :hosts %>

          <%= p.label :port %>
          <%= p.text_field :port, placeholder: :port %>

      <% end %>

      <%= f.submit 'Save changes', class: 'btn btn-large btn-primary' %>

<% end %>

Все работает нормально, и я могу собирать/сохранять/обновлять данные, однако в интерфейсе данные в хэше (который был ранее сохранен) не отображаются. Есть идеи, почему?

Кстати, на моем контроллере у меня есть следующее:

    @user = current_user
    if @user.update_attribute(:import_settings, params[:user][:import_settings])
        flash[:success] = 'Details saved'
        redirect_to admin_path
    else
        flash[:error] = 'An error happened'
    end

Это настраиваемый контроллер для обновления только части сведений о пользователе. (Не главная страница редактирования для пользователей).


person WagnerMatosUK    schedule 22.08.2014    source источник


Ответы (1)


Обновите свой код из

<%= f.fields_for :import_settings do |p| %>

to

<%= f.fields_for @user.import_settings do |p| %>
person RubyOnRails    schedule 23.08.2014