Skip to content

Instantly share code, notes, and snippets.

@salami-art
Created November 20, 2013 23:45
Show Gist options
  • Save salami-art/7573337 to your computer and use it in GitHub Desktop.
Save salami-art/7573337 to your computer and use it in GitHub Desktop.
'books' route called with redirect_to assigns variable as expected, Action :index (same page) called with render doesn't.
class BooksController < ApplicationController
def index
@books = Book.all # # this variable doesn't get loaded with " render "
respond_to do |format|
format.html
format.yaml { render text:@books.to_yaml }
format.json { render text:@books.to_json }
end
end
def show
@book = Book.find(params[:id])
respond_to do |format|
format.html
format.yaml { render text:@book.to_yaml }
end
end
def new
@book = Book.new
end
def create
@book = Book.new(book_params)
if @book.save
redirect_to book_path(@book)
else
render :new
end
end
def edit
@book = Book.find(params[:id])
end
def update
@book = Book.find(params[:id])
end
def destroy
@book = Book.find(params[:id])
@book.destroy
render :index #redirect_to books # here is where either render or redirect_to is called
end
private
def book_params
params.require(:book).permit(:title, :text, { author_attributes: [:name]})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment