Devise controllers rails

℡╲_俬逩灬. 提交于 2019-12-03 15:01:25

问题


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


来源:https://stackoverflow.com/questions/4545945/devise-controllers-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!