Devise的控制器在哪里? 如何在用户/编辑页面中添加其他模型的数据?
它似乎设计不在app / controller文件夹中创建任何控制器文件,现在我希望在用户/编辑视图中显示来自其他模型的一些自定义信息,但由于没有控制器要添加,因此无法弄清楚如何操作
@num_of_cars = current_user.num_of_cars.all
我希望在用户/编辑页面中显示num_of_cars,但无法弄清楚如何操作
编辑 – 下面提到的代码给我错误,我把这个代码放在我的设计/注册/编辑文件中
其中myinfo是另一个具有部分_cars_list.html.erb的资源,/ myinfo scaffold工作得非常好但是当我试图将那部分显示给用户/编辑时,它给了我
undefined method `each_with_index' for nil:NilClass
(我使用each_wit_index来显示列表,但我不认为这是问题)
覆盖Devise会话控制器操作:
# 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"}
您可以为handel devise/registration/edit
创建新的控制器
这是controller/passwords_controller.rb
class PasswordsController < Devise::RegistrationsController before_filter :authenticate_user! def edit @num_of_cars = current_user.num_of_cars.all super end def update @user = current_user # raise params.inspect if @user.update_with_password(params[:user]) sign_in(@user, :bypass => true) redirect_to user_path, :notice => "Password has been change!" else render :edit,:locals => { :resource => @user, :resource_name => "user" } end end end
这是routes.rb
devise_scope :user do get '/edit_password' => 'passwords#edit', :as => :change_password put '/change' => 'passwords#update' end
最后你可以将devise/registrations/edit.html.erb
复制到passwords/edit.html.erb
在您的情况下,您不必覆盖设计控制器。 你可以替换
<%= render partial: 'myinfo/cars_list' %>
同
<%= render partial: 'myinfo/cars_list', :locals => { :num_of_cars => current_user.num_of_cars.all } %>
在您的设计/注册/编辑页面中。 然后使用“num_of_cars”变量而不是“@num_of_cars”,在’myinfo / cars_list’部分内,应该解决您的问题。