Created
June 18, 2014 07:27
-
-
Save chatman-media/544bb399258f9dea34dc to your computer and use it in GitHub Desktop.
SO Answer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SectionsController < ApplicationController | |
| before_action :set_section, only: [:show, :edit, :update, :delete, :destroy] | |
| layout 'admin' | |
| def index | |
| @sections = Section.sorted | |
| end | |
| def show | |
| end | |
| def new | |
| @section = Section.new(:name => 'Default') | |
| set_pages | |
| end | |
| def create | |
| @section = Section.new(section_params) | |
| if @section.save | |
| flash[:notice] = "Section created successfully." | |
| redirect_to :action => 'index' | |
| else | |
| set_pages | |
| render 'new' | |
| end | |
| end | |
| def edit | |
| set_pages | |
| end | |
| def update | |
| if @section.update_attributes(section_params) | |
| flash[:notice] = "Section updated successfully." | |
| redirect_to :action => 'show', :id => @section.id | |
| else | |
| set_pages | |
| render 'edit' | |
| end | |
| end | |
| def delete | |
| end | |
| def destroy | |
| @section.destroy | |
| flash[:notice] = "Section destroyed successfully." | |
| redirect_to :action => 'index' | |
| end | |
| private | |
| def set_section | |
| @section = Section.find(params[:id]) | |
| end | |
| def set_pages | |
| @pages = Page.order('position ASC') | |
| @section_count = Section.count + (@section.new_record? ? 1 : 0) | |
| end | |
| def section_params | |
| params.require(:section).permit(:name, :position, :content) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, how do I nest Sections into the Page?
my Sections_Controller looks exactly like this. But it gives me Undefined error while I tried to nest Sections into Page.