2013-04-23 3 views
0

Я использую программу для аутентификации пользователей, и когда новый пользователь подписывается, я хотел бы также создать новую учетную запись и установить этого пользователя как владельца учетной записи. Я решил использовать форму с вложенной моделью для этого. Это моя установка:Rails3 + Devise + simple_form + nested_model

модели:

class User < ActiveRecord::Base 
    belongs_to :account 
    has_one :owned_account, :class=> Account, :foreign_key => :owner_id 
    accepts_nested_attributes_for :owned_account 
end 

class Account < ActiveRecord::Base 
    has_many :users 
    belongs_to :owner, :class => User, :foreign_key => :owner_id 
end 

маршруты:

devise_for :users, :path_prefix => 'authentication', :controllers => {:registrations => "registrations"} 
resources :accounts 

перекрываться регистрация Разрабатывают контроллер:

class RegistrationsController < Devise::RegistrationsController 
    def new 
    super 
    owned_account = resource.build_owned_account 
    end 

    def create 
    super 
    end 
end 

форма:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
    <%= f.error_notification %> 

    <div class="form-inputs"> 
    <%= f.input :email, :required => true, :autofocus => true %> 
    <%= f.input :password, :required => true %> 
    <%= f.input :password_confirmation, :required => true %> 

    <%= f.simple_fields_for :owned_account do |account| %> 
     <%= account.input :name, :required => true %> 
    <% end %> 
    </div> 

    <div class="form-actions"> 
    <%= f.button :submit, "Sign up" %> 
    </div> 
<% end %> 

Никакое исключение не возникает при доступе к пути sign_up, но вложенная часть формы не генерируется. Существует несколько подобных вопросов уже, но при условии решения не похоже на работу для меня ...

ответ

0

Проблема была с помощью super в перекрываться контроллера. Этот код работает:

class RegistrationsController < Devise::RegistrationsController 
    def new 
     resource = build_resource({}) 
     resource.build_owned_account 
     respond_with resource 
    end 

    def create 
    super 
    end 
end 
0

По вашему коду:

def new 
    super 
    owned_account = resource.build_owned_account 
end 

Переменная owned_account не доступна в вашем представлении. Как о замене этой линии с:

resource.owned_account = resource.build_owned_account 
Смежные вопросы