Rails - unique template for multiple controllers -
for application, need several controllers having more or less same behavior (with few particularities each). basically, have that:
# controllers/main.rb maincontroller < actioncontroller::base def show ... end def create ... end def destroy ... end end # controllers/first.rb firstcontroller < maincontroller helper_method :custom_stuff_one private def custom_stuff_one 'bli' end end # controllers/second.rb secondcontroller < maincontroller helper_method :custom_stuff_two private def custom_stuff_two 'bla' end end # routes.rb resources :first, :only => [:show, :create, :destroy] resources :second, :only => [:show, :create, :destroy]
that works fine, wasn't able have same simplicity templates. i'd simple as:
# views/main/show.html.erb <html> <body> here stuff in common controllers ... </body> </html> # views/first/show.html.erb <%= stylesheet_link_tag('my_css_only_for_first') %> <%= javascript_include_tag(custom_stuff_one) %> # views/second/show.html.erb <%= stylesheet_link_tag('another/css/file') %> <%= javascript_include_tag(custom_stuff_two) %>
and of course, when accessing /first/1 template rendered "views/main/show.html.erb" (and include tags "views/first/show.html.erb").
i'm getting bit lost yield, layouts etc... (and i'm wondering if i'm not doing wrong controller inheritance ...)
any ideas ?
note: we're using rails 3.2.17, ruby 2.1.1
cheers, vincent
with controller have, if query /first/1, render /first/show , not consider main/show. want use layout, layout piece of html code reusable between controllers.
you should create file called layouts/layout_name.html.erb :
<html> <head> <%= yield :head %> <head> <body> <%=yield%> </body> </html>
on controllers, add
# controllers/first.rb firstcontroller < maincontroller helper_method :custom_stuff_one layout :layout_name ... end
and on views first :
<%=content_for :head do%> <%= stylesheet_link_tag('my_css_only_for_first') %> #this display in head <%end%> <div>foo</div> #this display in body
Comments
Post a Comment