2012-03-18 23 views
0

私はhas_many:usersというコミュニティモデルとbelongs_to:communityというユーザーモデルを持っています。デベロッパーへの登録

サインアップするときに同じプロセスでコミュニティを作成し、サインアップするときにこのコミュニティに割り当てることができます。コミュニティが作成されていない場合は、ユーザーを作成しないでください。

どうすればよいですか?おそらくユーザーのためのカスタムデベロッパーコントローラを作成する...任意のアイデアをいただければ幸いです。

アプリ/コントローラ/工夫/ 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:最初の応答の変化後

おかげ


。エルブ

<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

+0

私はDevise 2.0.4を使用しています – Daniel

答えて

0

はい、確かに、私はあなたの工夫コントローラーをカスタマイズすることがあると思います。

step1。

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

STEP2:(独自の発電機のrakeタスクを実行することによって、または手動で、ローカルフォルダにそのコントローラーをコピーして)お近くの工夫コントローラファイルを作成し、その後、フォルダ構造は次のようになります。

if resource.save # change this line of code. 

に、

# 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 Community.save_both_a_new_user_and_a_new_instance(resource) 

ステップ3/registrations_controller.rbを工夫/あなたのアプリ/コントローラを編集します。トランザクションを使用してコミュニティでメソッドを実装する:

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に変更して変更を行いました。私はエラーが発生します:DevEyeのNameError :: RegistrationsController#new #のための未定義のローカル変数またはメソッド 'require_no_authentication ' – Daniel

+0

明示的なコード(コントローラ/ビュー/ Gemfile)を追加してください元の投稿ですか? –

+0

なぜロガー情報が表示されないのですか?あなたはdeviseコントローラを生成するレーキタスクが何であるか教えてください。私はそれを見つけることができません。ありがとう! – Daniel

0

もう一度やり直してください!私はあなたのコードから変更しなければならなかった唯一のものは以下の通りであった。

  • は、(ユーザーが)self.save_both_a_new_user_and_a_new_instanceする(にhas_manyを使用するため)のActiveRecordからコミュニティを継承し、
  • 変更セルフクラスメソッドSelf.save_both_a_new_user_and_a_new_instance(ユーザー)。

ありがとう!!