Ruby on rails 设计的控制器在哪里?如何在用户/编辑页面中添加来自其他模型的数据?
似乎Deavie没有在app/controller文件夹中创建任何控制器文件,现在我想在users/edit视图中显示来自其他模型的一些自定义信息,但由于没有要添加的控制器,因此无法确定如何执行此操作Ruby on rails 设计的控制器在哪里?如何在用户/编辑页面中添加来自其他模型的数据?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,似乎Deavie没有在app/controller文件夹中创建任何控制器文件,现在我想在users/edit视图中显示来自其他模型的一些自定义信息,但由于没有要添加的控制器,因此无法确定如何执行此操作 @num_of_cars = current_user.num_of_cars.all 我想在用户/编辑页面中显示汽车的数量,但不知道怎么做 编辑-下面提到的代码给了我错误,我把这个代码放在我的设计/注册/编辑文件中 <%= render partial: 'myinfo/cars_lis
@num_of_cars = current_user.num_of_cars.all
我想在用户/编辑页面中显示汽车的数量,但不知道怎么做
编辑-下面提到的代码给了我错误,我把这个代码放在我的设计/注册/编辑文件中
<%= render partial: 'myinfo/cars_list' %>
(我使用每个索引来显示列表,但我不认为这是问题所在)覆盖设计会话控制器操作:
# 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
您可以为handel创建新控制器
designe/registration/edit
这是controller/passwords\u 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
最后,您可以将
design/registrations/edit.html.erb
复制到passwords/edit.html.erb
在您的情况下,您不必覆盖designe控制器。你可以替换
<%= render partial: 'myinfo/cars_list' %>
与
{:num_of_cars=>current_user.num_of_cars.all}%>
在您的设计/注册/编辑页面中。然后在“myinfo/cars\u列表”部分使用“num\u of\u cars”变量而不是“@num\u of\u cars”,应该可以解决您的问题。谢谢,您能告诉我控制器的regsiter\u是什么意思吗?我认为在应用程序中有控制器代码就足以链接到路由?也许在某些情况下,在你的新应用程序中调用“super”是合适的controller@ChristophGeschwind-谢谢Chris,你能解释一下super如何合适吗?还有,为什么要为:routes中的用户定义控制器?@ChristophGeschwind and zeantsoi-上面的代码给了我一个错误,我把这个代码放在我的设计/注册/编辑文件中,其中myinfo是另一个带有部分_cars_list.html.erb的资源,/myinfo scaffold工作得非常好,但当我试图在用户/编辑中显示该部分时,它为nil:NilClass提供了未定义的方法“each_with_index”(我使用each_wit_index来显示列表,但我认为这不是问题)@iCyborg,在变量赋值后调用
super
,将使操作能够继承要重写的操作的属性。在这种情况下,这意味着您的编辑
操作将执行与默认设计编辑
操作相同的操作,但您的视图中也会有@num\u of\u cars
实例变量。感谢您的回复,代码中出现了一些错误,因为我得到了nil:NilClass的未定义方法“each_with_index”-当我试图在其他视图中呈现部分时,我试图运行一个测试代码,在designe/registrations/edit.html.erb中,在designe/registration/_car_list.html.erb中,我有456,在app/controllers/session_controller.rb中,我有@test_var=123的edit方法,但是当我查看/users/edit时,它只显示456而没有读取123(我在controller中设置的变量),你知道我做错了什么吗?thanksI正在尝试运行一个测试代码,在designe/registrations/edit.html.erb中,我有,在designe/registration/_car_list.html.erb中,我有456,在app/controllers/session_controller.rb中,我有@test_var=123的edit方法,但当我查看/用户/编辑时,它只显示456,而没有读取123(我在controller中设置的变量),知道我做错了什么吗?感谢您正在“app/controllers/session\u controller.rb”中设置值,编辑将参考“app/controllers/registrations\u controller.rb的编辑方法”。当您在错误的控制器操作中设置时,它将“@test_var”视为nil,因此不显示任何内容。您可以在服务器日志(终端)中查看您的请求将执行哪个控制器操作。Prem,感谢您的回复,但是没有注册控制器,因为我正在使用上面的@zeantsoi方法为:users的designe_,:controllers=>{:sessions=>“sessions”},这不是正确的方法吗?根据我的想法,这个变量(@num_of_cars)仅当您覆盖“注册\控制器”时才可用。如果这是您想要使用的唯一东西,那么您可以将该值作为局部变量从视图(设计/注册/编辑)传递给partial,正如我前面提到的。
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
devise_scope :user do
get '/edit_password' => 'passwords#edit', :as => :change_password
put '/change' => 'passwords#update'
end
<%= render partial: 'myinfo/cars_list' %>
<%= render partial: 'myinfo/cars_list', :locals => { :num_of_cars => current_user.num_of_cars.all } %>