可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm using Rails 3, on ruby 1.8.7. And using for auth. devise (1.1.3). But it is a quite large community site i'm building, so i have a table for profiles and a table for users. And every time a user registers it should generate a profile also, but in devise i'm not allowed the controllers, so i'm completely lost..
Edit
Now it says
undefined method `getlocal' for Tue, 28 Dec 2010 11:18:55 +0000:DateTime
Then when i make a file in lib called date_time.rb with this code
class DateTime def getlocal "it works" end end
And require it in my Application controller it gives me this
fail wrong number of arguments (1 for 0)
it is like it don't know anything called devise anymore, but in my routes i do have devise
devise_for :users
回答1:
You can subclass the Devise RegistrationsController and add your own logic in the create() method, and call the parent class methods for everything else.
class MyRegistrationsController < Devise::RegistrationsController prepend_view_path "app/views/devise" def create super # Generate your profile here # ... end def update super end end
If you want to customise the Devise views that are packaged inside the Gem then you can run the following command to generate the view files for your app:
rails generate devise:views
You will also need to tell the router to use your new controller; something like:
devise_for :users, :controllers => { :registrations => "my_registrations" }
回答2:
There's not really any need to involve the controller in this; models can (and should) do all of the heavy lifting here.
I'm assuming that you have a relationship between User
and Profile
models, in which case, you should just be able to do something like this:
class User < ActiveRecord::Base has_one :profile # could be a belongs_to, but has_one makes more sense after_create :create_user_profile def create_user_profile create_profile(:column => 'value', ...) end end