Wasi
Wasi

Reputation: 1246

Current object in rails code

If i have a controller called Ece. It stores many courses. Now if a user is on a show page of a particular course. How would i know which Ece object's show page is displayed. That is i want to access the current Ece object. Ultimately i want to do this to the current ece object

@code = current_ece.course_code # assuming current_ece means the current object

I want to use @code for other purposes.

Where course_code is one of the columns. I want to do this only on the current Ece object

Upvotes: 0

Views: 324

Answers (1)

jtbandes
jtbandes

Reputation: 118651

Normally in your show action you will grab the current object and put it in an instance variable:

def show
  @ece = Ece.find(params[:id])
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :json => @ece }
  end
end

Then you can just use @ece inside your view. Read the Getting Started with Rails [Rails 3/edge link] guide for more information.

Upvotes: 1

Related Questions