Devise的控制器在哪里?如何在用户/编辑页面中添加来自其他模型的数据? [英] Where is the controller of Devise ? how to add data from other models in users/edit page?

查看:24
本文介绍了Devise的控制器在哪里?如何在用户/编辑页面中添加来自其他模型的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎设计不在 app/controller 文件夹中创建任何控制器文件,现在我希望在用户/编辑视图中显示来自其他模型的一些自定义信息,但由于没有控制器,因此无法弄清楚如何去做添加

it seems devise do not create any controller file in app/controller folder, now I am looking to show some custom info from other models in users/edit view but not able to figure out how to do it since there is no controller to add

@num_of_cars = current_user.num_of_cars.all

我想在用户/编辑页面中显示 num_of_cars 但不知道如何去做

I am looking to show num_of_cars in users/edit page but not able to figure out how to do it

编辑 - 下面提到的代码给了我错误,我将此代码放在我的设计/注册/编辑文件中

EDIT - the below mentioned code giving me error, I am putting this code in my devise/registration/edit file

<%= render partial: 'myinfo/cars_list' %> 

其中 myinfo 是另一个具有部分 _cars_list.html.erb 的资源,/myinfo 脚手架工作得非常好,但是当我尝试将该部分显示到用户/编辑中时,它给了我

where myinfo is another resource with a partial _cars_list.html.erb, the /myinfo scaffold works perfectly fine but when I am trying to display that partial into users/edit, it is giving me

undefined method `each_with_index' for nil:NilClass  

(我使用 each_wit_index 来显示列表,但我认为这不是问题)

(I am using each_wit_index to display list but I don't think that is the problem)

推荐答案

覆盖设计会话控制器操作:

Override the Devise sessions controller actions:

# app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController

  def edit
    # add custom logic here
    @num_of_cars = current_user.num_of_cars.all 
    super
  end

end 

注册控制器:

# app/config/routes.rb
devise_for :users, :controllers => {:sessions => "sessions"}

这篇关于Devise的控制器在哪里?如何在用户/编辑页面中添加来自其他模型的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆