Rails, same view, different controllers

◇◆丶佛笑我妖孽 提交于 2019-12-12 01:26:06

问题


map.resources :persons

map.resources :people

class Persons_controller < ApplicationController::Base
  #the whole logic for the controller
end

class People_controller < PersonsController
  #nothing special there
end

How I can use the views from /app/views/persons/ when I access my app from http://mydomain.com/people/1 ?

I get an error about missing people/show.erb view , I don't want use any symlinks


回答1:


http://api.rubyonrails.org/classes/ActionView/Partials.html

In your views i'd suggest creating a shared folder to store shared views.

/views/shared
/views/shared/_people.html.haml

Then you can do something like where you want to use that partial.

render :partial => "shared/people"

You still need to create the action views in each directory but just render the shared partial instead.




回答2:


I think the right thing to do is follow David Lyod's advice. It's much better organization and you always know where your shared files are. However, you can still use the same concept by placing the partial _people.html.erb in the views/persons directory. Then in the people view file that uses it

render :partial => "persons/people"

Hope this helps.



来源:https://stackoverflow.com/questions/3616926/rails-same-view-different-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!