设计控制器导轨

Olu*_*sen 4 ruby-on-rails controllers devise

我在ruby 1.8.7上使用Rails 3.并使用auth.设计(1.1.3).但它是一个非常大的社区网站,我正在建设,所以我有一个用于配置文件的表和一个用户表.每次用户注册它也应该生成一个配置文件,但在设计中我不允许控制器,所以我完全迷失了..

编辑

现在它说

undefined method `getlocal' for Tue, 28 Dec 2010 11:18:55 +0000:DateTime
Run Code Online (Sandbox Code Playgroud)

然后,当我使用此代码在lib中创建名为date_time.rb的文件时

class DateTime
  def getlocal
    "it works"
  end
end
Run Code Online (Sandbox Code Playgroud)

并在我的应用程序控制器中要求它给我这个

fail wrong number of arguments (1 for 0)
Run Code Online (Sandbox Code Playgroud)

它就像它不知道任何叫做设计的东西,但在我的路线中我确实有设计

devise_for :users
Run Code Online (Sandbox Code Playgroud)

Sco*_*ott 15

您可以继承Devise RegistrationsController并在create()方法中添加自己的逻辑,并为其他所有内容调用父类方法.

class MyRegistrationsController < Devise::RegistrationsController
  prepend_view_path "app/views/devise"

  def create
    super
    # Generate your profile here
    # ...
  end

  def update
    super
  end
end
Run Code Online (Sandbox Code Playgroud)

如果要自定义Gem中打包的Devise视图,则可以运行以下命令为应用程序生成视图文件:

rails generate devise:views
Run Code Online (Sandbox Code Playgroud)

您还需要告诉路由器使用您的新控制器; 就像是:

devise_for :users, :controllers => { :registrations => "my_registrations" }
Run Code Online (Sandbox Code Playgroud)


von*_*rad 6

实际上没有必要让控制器参与其中; 模型可以(并且应该)完成所有繁重的工作.

我假设你UserProfile模特之间有关系,在这种情况下,你应该能够做到这样的事情:

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
Run Code Online (Sandbox Code Playgroud)