简体   繁体   English

我可以在Rails 4中从当前控制器渲染另一个控制器的动作吗?

[英]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 创建usersschools都通用的局部视图(_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.

相关问题 如何在Rails中使用对象的控制器的create动作用对象的新实例渲染局部对象? - How can I render a partial with a new instance of my object from the create action of that object's controller in Rails? Rails 3:如何从另一个控制器的视图呈现动作 - Rails 3: How to render an action from another controller's view 我可以从我的 controller 渲染 rails 欢迎页面吗? - Can I render rails welcome page from my controller? 从另一个控制器渲染控制器操作 - Render controller action from another controller 在Rails中,从另一个控制器的角度渲染一个动作方法 - In rails render an action method from a view of another controller Rails - 如何将创建/新操作从一个控制器放入另一个控制器的索引? - Rails - How can I put the create/new action from one controller into the index of another controller? Rails:从控制器调用另一个控制器动作 - Rails: call another controller action from a controller Rails功能测试 - 我无法在功能测试中从另一个控制器的视图断言模板 - Rails Functional Tests - I can't assert a template from another controller's view in my functional test 我可以在另一个动作中调用动作(在rails控制器中)吗? - Can I call an action in another action (in a rails controller)? Rails调用另一个控制器的动作 - Rails call another controller's action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM