##Set pages as header and footer navigations in Rails.
I am not sure if this is the best solution, I made this up while working on a project but I hope it helps someone.
Assuming you already have a page model and can CRUD pages.
rails g migration add_nav_to_pages
db/migrate/XXX_add_nav_to_pages.rb
class AddNavToPages < ActiveRecord::Migration
def change
add_column :pages, :nav, :string
end
end
rake db:migrate
models/page.rb
class Page < ActiveRecord::Base
scope :header, -> { where nav: 'header'}
scope :footer, -> { where nav: 'footer'}
end
This could be your new and edit forms. I created a partial for those therefore:
views/pages/_form.html.erb
... form
<%= f.label :nav, 'Navigation' %>
<%= f.select(:nav, options_for_select([['header'], ['footer'], ['none']],
:selected => f.object.nav)) %>
... rest of the form
and finally
Wherever you are displaying the 'header' navigation.
In my case views/application/_header.html.erb
<% Page.header.each do |page| %>
<li><%= link_to page.title, page_path(page) %></li>
<% end %>
Wherever you are displaying the 'footer' navigation.
In my case views/application/_footer.html.erb
<% Page.footer.each do |page| %>
<li><%= link_to page.title, page_path(page) %></li>
<% end %>
and in the main layouts file
views/layouts/application.html.erb
...
<%= render 'header'%>
<%= yield %>
<%= render 'footer'%>
...
and don't forget to permit :nav in your pages controller
private
...
def page_params
params.require(:page).permit(:title, :body, :nav)
end