Jeremy Walters
Jeremy Walters

Reputation: 29

How to access variables and methods across controllers in Rails

Books Controller:

 class BooksController < ApplicationController
 before_action :set_book, only: [:show, :edit, :update, :destroy]

 # GET /books
 # GET /books.json
 def index
 if params[:student_id]
  student = Student.find(params[:student_id])
  @books = student.books

 else
  @books = Book.all
 end
 respond_to do |format|
  format.html
  format.csv {render text: @books.to_csv }

  end
end





  def show

  end


  def new
   @book = Book.new
  end


  def edit
  end


  def create
  @book = Book.new(book_params)
  respond_to do |format|
  if @book.save
    format.html { redirect_to @book, notice: 'Book was successfully created.'    }
    format.json { render :show, status: :created, location: @book }
  else
    format.html { render :new }
    format.json { render json: @book.errors, status: :unprocessable_entity }
  end
end
 end


  def update
    respond_to do |format|
  if @book.update(book_params)
    format.html { redirect_to @book, notice: 'Book was successfully updated.' }
    format.json { render :show, status: :ok, location: @book }
  else
    format.html { render :edit }
    format.json { render json: @book.errors, status: :unprocessable_entity }
    end
  end
end


def destroy
  @book.destroy
  respond_to do |format|
  format.html { redirect_to books_url, notice: 'Book was successfully       destroyed.' }
  format.json { head :no_content }
end
end

private
  # Use callbacks to share common setup or constraints between actions.
def set_book
  @book = Book.find(params[:id])
end

 # Never trust parameters from the scary internet, only allow the white list through.
def book_params
  params.require(:book).permit(:book_name, :book_level, :total_words,  :words_wrong, :self_corrections, :student_id)
end

 end

Here is my "Students Controller"

class StudentsController < ApplicationController

def show
    @student = Student.find(params[:id]) rescue nil
    @books = Book.where(student_id: params[:id])
    @book = Book.new  


end

def create
    student = Student.new(student_parameters)
    student.user_id = current_user.id
    if student.save
        redirect_to student
    else
        redirect_to 'students#index'
    end
  end


def index
    @students = Student.where("user_id = ?",current_user.id)
    @student = Student.new  
end

private

def student_parameters
    params.require(:student).permit(:first_name, :last_name)
end





end

Books belong to students, and on the index view where I show an individual's students books, I want the heading at the top of the page to say "{current student}'s book". I'm unsure as to how to call the name of the current student, and I think the source of my confusion is the fact that I'm working with the books controller and student.first_name and student.last_name aren't available to me.

Additionally, I would like to know how to access book data when I'm using the students controller. For instance when I'm at localhost:3000/students/2, I'd like to show all that students books.

I'm looking for something like current_student.books or current_student.name, I think, but I'm not sure how to create them.

Upvotes: 2

Views: 915

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

Instead of doing...

student = Student.find(params[:student_id])
  @books = student.books

do...

@student = Student.find(params[:student_id])
  @books = @student.books

This gives you the instance variable @student that you can use in your views, in particular @student.first_name and @student.last_name

you may want to condition the code in the view so that it only shows if @student is not nil (it would be nil if params[:student_id] wasn't passed).

Upvotes: 1

Related Questions