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

时间:2022-06-01 17:59:55

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而不是所需的学校/ create_person.html.erb

2 个解决方案

#1


-1  

Check your routes and see if you have the method create_person in schools routes. If positive try redirect_to:

检查您的路线,看看您是否在学校路线中使用create_person方法。如果是肯定的尝试redirect_to:

redirect new_school_person_path

with new_school_person_path as the route to that method.

使用new_school_person_path作为该方法的路由。

#2


0  

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

创建一个局部视图(_create_person.html.erb),这对于用户和学校都很常见

in users folder new.html.erb

在用户文件夹new.html.erb中

<%= render partial: "schools/create_person" %>

<%= 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 #depends在你的路线上定义

#1


-1  

Check your routes and see if you have the method create_person in schools routes. If positive try redirect_to:

检查您的路线,看看您是否在学校路线中使用create_person方法。如果是肯定的尝试redirect_to:

redirect new_school_person_path

with new_school_person_path as the route to that method.

使用new_school_person_path作为该方法的路由。

#2


0  

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

创建一个局部视图(_create_person.html.erb),这对于用户和学校都很常见

in users folder new.html.erb

在用户文件夹new.html.erb中

<%= render partial: "schools/create_person" %>

<%= 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 #depends在你的路线上定义