1

I have a User model that I am using devise for. Each User belongs_to a Group. I am trying to find an easy way to set a user's group on sign up - without creating a custom controller to override devise?.

My models look like this:

class User < ActiveRecord::Base
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :users
end

Here's the form:

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

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

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

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

<%= label_tag :group_name %>
<%= text_field_tag :group_name %>

<%= f.submit "Sign up" %>

Since I'm only trying to save this one additional attribute (group_name), I would like to avoid creating a whole new custom devise controller. Is there a way to save a user's group at the model level - with a callback maybe? Or is there a better way to save a group on sign up?

Thanks!

4

1 に答える 1

2

上記のようにカスタマイズされたフォームを既に使用している場合 (つまり、Devise が提供するフォームを使用していない場合)、Devise コントローラーをオーバーライドする必要はありません。グループは、他の一括割り当て可能な属性とともに一括割り当てされます。

(個人的には、ユーザー エクスペリエンスの観点から、グループ選択をドロップ ダウンか何かにして、使いやすくするのが良いかもしれませんが、それは単なる好みの問題です。オートフィルも良いかもしれません。)

于 2012-07-08T05:13:12.743 に答える