notGeek
notGeek

Reputation: 1528

View inside other view

In a Rails application I have a view index.html.erb and a controller posts_controller.rb. In the controller I have defined a variable:

def index
@posts = TableName.find...

and in the view I have:

<% @posts.each do |post| %>

If I access it via localhost/posts/index it works fine. But what I really want is to show this view inside other views so I'm using <%= render :template => 'posts/index' %> but I get this error message: "NoMethodError - You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each"

How can I do this? Thanks

Upvotes: 0

Views: 59

Answers (1)

kevinhyunilkim
kevinhyunilkim

Reputation: 864

NoMethodError is triggered when you try to call methods on undefined object. In other word, you render posts/index in other views, but @posts instance variable is not defined in the other view. render template does not route the controller.

Upvotes: 2

Related Questions