2011-01-07 15 views
2

私はユーザー登録にDeviseを使用しています。私はDeviseのカスタマイズについてすべてfamous tutorialを読んできましたが、この単純な作業を理解することはできません。彼のモデル(HABTM)に従いましたRails 3:役割のチェックボックスを登録フォームに追加する

Devise編集フォームに役割チェックボックスを追加したいと思います。私はコントローラーを持っていませんDeviseは提供していませんが、新しいユーザーにデフォルトの役割を追加することができました。正しい情報をチェックしてチェックボックスを表示することはできましたが、編集することはできません(anydataを保存しません)。カスタムコントローラが必要ですか?はいの場合、どのくらい正確ですか?私はHABTMの関係を初めて知りました!

マイUserモデル

class User < ActiveRecord::Base 
    has_and_belongs_to_many :roles 

    before_save :setup_role 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, :lockable and :timeoutable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 

    def role?(role_sym) 
    roles.any? { |r| r.name.underscore.to_sym == role_sym } 
    end 

    # Default role is "User" 
    def setup_role 
    if self.role_ids.empty?  
     self.role_ids = [3] 
    end 
    end 


end 

私の編集フォーム(工夫/登録/ edit.html.rb

<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! %> 

    <p><%= f.label :email %><br /> 
    <%= f.text_field :email %></p> 

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

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

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

    <% for role in Role.find(:all) %> 
     <div> 
      <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %> 
      <%= role.name %> 
     </div> 
    <% end %> 

    <p><%= f.submit "Update" %></p> 
<% 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 %> 

答えて

1

はあなたのコンソールをチェックし、私は「できません質量割り当て」エラーを得ました。 、私はrole_idsをユーザモデルのattr_accessibleに入れて、うまくいった。

関連する問題