[英]Can I render another controller's action from my current controller in Rails 4?
Might be a silly one but is it still possible to render an action from another controller in Rails 4? 可能是一个愚蠢的人,但是仍然可以从Rails 4中的另一个控制器进行动作吗?
Like this in UsersController: 在UsersController中是这样的:
def new
@person = User.new
render 'schools/create_person'
end
For some reason it keeps rendering the new.html.erb instead of desired schools/create_person.html.erb 由于某些原因,它会继续呈现new.html.erb而不是所需的school / create_person.html.erb
It did not clear me what do you want 并没有告诉我你想要什么
but I can suggest one way if you want to render the view 但是我可以建议一种方法来渲染视图
create a partial view (_create_person.html.erb) which are common for both users
and schools
and then 创建users
和schools
都通用的局部视图(_create_person.html.erb),然后
in users
folder new.html.erb
在users
文件夹new.html.erb
<%= render partial: "schools/create_person" %>
better you can make the partial view in shared folder in your views folder and render it 更好的是,您可以在视图文件夹的共享文件夹中创建部分视图并进行渲染
If you want to redirect 如果要重定向
redirect_to create_person_schools_url
#depends on your route define redirect_to create_person_schools_url
#取决于您的路线定义
Check your routes and see if you have the method create_person in schools routes. 检查您的路线,并查看学校路线中是否具有create_person方法。 If positive try redirect_to : 如果是肯定的,请尝试redirect_to :
redirect new_school_person_path
with new_school_person_path
as the route to that method. 使用new_school_person_path
作为该方法的路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.