开发者

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 curren开发者_StackOverflow中文版t 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜