2012-03-18 143 views
0

我有一个社区模型has_many:用户和用户模型belongs_to:社区。注册wih设计

我需要用户注册时可以在同一个过程中创建一个社区,并在注册时分配给此社区。如果没有创建社区,则不应创建该用户。

我应该怎么做?也许为用户创建一个定制的设计控制器...任何想法,将不胜感激。

由于


第一响应的更改后:

应用程序/控制器/设计/ registrations_controller.rb

# GET /resource/sign_up 
    def new 
     logger.info "1" 
     build_resource({}) 
     render_with_scope :new 
    end 

# POST /resource 
def create 
    logger.info "2" 
    build_resource 

    if Community.save_both_a_new_user_and_a_new_instance(resource) 
    if resource.active_for_authentication? 
     set_flash_message :notice, :signed_up if is_navigational_format? 
     sign_in(resource_name, resource) 
     respond_with resource, :location => redirect_location(resource_name, resource) 
    else 
     set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format? 
     expire_session_data_after_sign_in! 
     respond_with resource, :location => after_inactive_sign_up_path_for(resource) 
    end 
    else 
    clean_up_passwords(resource) 
    respond_with_navigational(resource) { render_with_scope :new } 
    end 
end 

应用/视图/设计/注册/ new.html。 erb

<h2>Sign up</h2> 

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <div><%= f.label :email %><br /> 
    <%= f.email_field :email %></div> 

    <div><%= f.label :password %><br /> 
    <%= f.password_field :password %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></div> 
</br> 
<h4>Introduce la contraseña de tu comunidad</h4> 
<div><%= f.number_field :community_id %></div> 
<p>o Crea una nueva comunidad</p> 

</br> 
<div><%= f.submit "Sign up" %></div> 
<% end %> 

<%= render "links" %> 

应用程序/视图/设计/注册/ edit.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2> 

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> 
    <%= devise_error_messages! %> 

    <div><%= f.label :email %><br /> 
    <%= f.email_field :email %></div> 

    <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> 
    <%= f.password_field :password, :autocomplete => "off" %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></div> 

    <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 
    <%= f.password_field :current_password %></div> 

    <div><%= f.submit "Update" %></div> 
<% end %> 

<h3>Cancel my account</h3> 

<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p> 

<%= link_to "Back", :back %> 

的Gemfile:

source 'https://rubygems.org' 
gem 'rails', '3.2.2' 
gem 'sqlite3' 
gem 'json' 
gem 'execjs' 
gem 'therubyracer' 
gem 'devise' 
gem "cancan" 
gem 'paperclip' 
gem "aws-s3" 
gem 'pg' 
gem 'twitter-bootstrap-rails' 

group :assets do 
    gem 'sass-rails', '~> 3.2.3' 
    gem 'coffee-rails', '~> 3.2.1' 
    gem 'uglifier', '>= 1.0.3' 
end 

gem 'jquery-rails' 
+0

未显示记录器信息 – Daniel 2012-03-19 23:06:54

+0

我正在使用Devise 2.0.4 – Daniel 2012-03-19 23:23:27

回答

0

是的,当然,我认为你必须自定义设计控制器。

step1。在你的本地控制器设计文件(通过运行其自己的发电机耙任务,或通过手动复制它的控制器到本地文件夹),在这之后,你的文件夹结构应该是这样的:

app/controllers/devise/registrations_controller.rb 
app/views/devise/registrations/new.html.erb 
app/views/devise/registrations/edit.html.erb 

第二步。编辑您的应用程序/控制器/设计/ registrations_controller.rb:

# POST /resource 
def create 
    build_resource 
    if resource.save # change this line of code. 
    if resource.active_for_authentication? 
     redirect_to root_path, :notice => "successfully registered..." 
    else 
     # other code .... 
    end 
    else 
    clean_up_passwords resource 
    respond_with resource 
    end 
end 

改变上面的文件,

if resource.save # change this line of code. 

到:

if Community.save_both_a_new_user_and_a_new_instance(resource) 

第三步。在社区使用交易实施方法:

class Community 
    def Self.save_both_a_new_user_and_a_new_instance(user) 
    Community.transaction do 
     community = Community.new(:name => "new community") 
     user.community = community 
     community.save! 
     user.save! 
    end 
    end 
end 
+0

谢谢!我生成了设计视图,然后从https://github.com/plataformatec/devise/blob/v1.3.0/app/controllers/devise/registrations_controller复制了registrations_controller。rb到app/controllers/devise并做了更改。我得到错误:NameError in Devise :: RegistrationsController#new 未定义的局部变量或方法'require_no_authentication'为# Daniel 2012-03-19 22:31:39

+0

请问您可以添加您的显式代码(我的意思是controllers/views/Gemfile)你原来的帖子? – 2012-03-19 22:44:36

+0

为什么不显示记录器信息的任何想法?你能告诉我哪个是rake任务来生成设计控制器吗?我找不到它。谢谢! – Daniel 2012-03-20 20:09:05

0

我重新做了一切,并工作!唯一的东西,我不得不从您的代码更改为:

  • 继承社区ActiveRecord的(使用的has_many)和
  • 改变自我类方法Self.save_both_a_new_user_and_a_new_instance(用户)self.save_both_a_new_user_and_a_new_instance(用户)。

谢谢!!