使用设计注册

Posted

技术标签:

【中文标题】使用设计注册【英文标题】:Signing up wih devise 【发布时间】:2012-03-18 21:53:56 【问题描述】:

我有一个拥有_many :users 的社区模型和一个属于_to :community 的用户模型。

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

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

谢谢


第一反应改变后:

app/controllers/devise/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

app/views/devise/registrations/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" %>

app/views/devise/registrations/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 %>

宝石文件:

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'

【问题讨论】:

记录器信息不显示 【参考方案1】:

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

第 1 步。创建本地 Devise 控制器文件(通过运行自己的生成器 rake 任务或手动将其控制器复制到本地文件夹),之后,您的文件夹结构应如下所示:

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

第二步。编辑你的 app/controllers/devise/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)

第 3 步。使用事务实现社区中的方法:

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

【讨论】:

谢谢!我生成了设计视图,然后将 registrations_controller 从 github.com/plataformatec/devise/blob/v1.3.0/app/controllers/… 复制到 app/controllers/devise 并进行了更改。我收到错误:Devise::RegistrationsController#new undefined local variable or method `require_no_authentication' for #<:registrationscontroller:0xbb8760c> 中的 NameError 能否将您的显式代码(我的意思是控制器/视图/Gemfile)添加到您的原始帖子中? 知道为什么不显示记录器信息吗?您能否告诉我生成设计控制器的 rake 任务是什么?我找不到它。谢谢! 你能正确访问“新”操作吗?我的意思是在访问/users/sign_up url 之后,应该触发“new”操作,并且应该将日志写入“development.log”(取决于您的 RAILS_ENV)。如果您发现页面(/users/sign_up)打开没有错误,但没有写入日志,可能是您没有成功“覆盖”设备的控制器,在这种情况下,请检查您的本地文件夹结构。【参考方案2】:

我重新做一遍并且工作!我必须从您的代码中更改的唯一内容是:

从 ActiveRecord 继承社区(用于使用 has_many)和 将 self 类方法 Self.save_both_a_new_user_and_a_new_instance(user) 更改为 self.save_both_a_new_user_and_a_new_instance(user)。

谢谢!!

【讨论】:

以上是关于使用设计注册的主要内容,如果未能解决你的问题,请参考以下文章

PHP设计模式 -- 注册模式

.net设计页面,使用 验证控件(默认启用)有登录和注册按钮按登录按钮的时候启用,注册不使用,怎么弄

设计 - 用回形针注册(缺少图像)

博客设计

仅使用设计和电子邮件注册电子邮件

在注册页面添加验证消息