ユーザー サインアップ用のネストされたモデル フォームがあり、ユーザーと場所を同時に作成します。ロケーションを作成したユーザーに管理者ロールが自動的に割り当てられるようにします。私はdevise/cancan/rolifyを使っています。これを行う方法についてのアイデアはありますか?
能力モデル
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? (:admin, Location.first)
can :manage, :all
end
ロケーションモデル
class Location < ActiveRecord::Base
resourcify
has_and_belongs_to_many :users
accepts_nested_attributes_for :users, :allow_destroy => true
attr_accessible :lat, :long, :name, :street_address, :places_id, :user_attributes
validates_uniqueness_of :places_id, :message => "location already taken"
end
ユーザーモデル
class User < ActiveRecord::Base
has_and_belongs_to_many :locations
accepts_nested_attributes_for :location
rolify
resourcify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :location_attributes
end
ロールモデル
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
scopify
end
最後に、サインアップ用のフォーム
<% resource.build_location %>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %>
<%= f.error_notification %>
<%= f.fields_for :location do |location_form| %>
<%= location_form.text_field :name, "data-geo" => "name" %>
<%= location_form.text_field :places_id, "data-geo" => "id" %>
<% end %>
<%= f.input :name, :autofocus => true %>
<%= f.input :email, :required => true %>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
<%= f.button :submit, 'Sign up', :class => 'btn-primary' %>
<% end %>
<%= render "devise/shared/links" %>