3

I have 2 models named Subject & Page. there is one to many association between them.

class Subject < ActiveRecord::Base

  has_many :pages
  attr_accessible :name
  attr_accessible :position
  attr_accessible :visible
  attr_accessible :created_at
end

and

class Page < ActiveRecord::Base

  belongs_to :subject                                                    
   attr_accessible :subject_id
   attr_accessible :name
   attr_accessible :permalink
   attr_accessible :position
   attr_accessible :visible
   attr_accessible :created_at
end

I have list.html.erb in view -> pages folder.

Q. My qus is I want to show all the subject_id in list.html.erb. how? for that which changes I have to do in pages_controller & list.html.erb so that I will got solution...

1 Answer 1

1

You can access any Model in any Controller in following way

   @instance_variable = ModelClass.all

In your case it should be something like following

pages_controller.rb

def list 
  @subjects_list = Subject.all
end

app/views/pages/list.html.erb

<% for subject in @subjects_list %>
  <!--  Your Code Here  -->
<% end %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.