Muralikrishna
Muralikrishna

Reputation: 245

How to use one controller view as a partial view for another controller in Ruby on Rails

Is it possible to use one controller view as a partial view of another controller view...

In my scenario my controller has an action method index and another controller having an action method index as follows.

First controller:

class TemplateItemsController < ApplicationController
def index
@template_items = TemplateItem.order('"TemplateGroup_id"')

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @template_items }
end
end

In the view, we display templateitems.

My second controller is as follows.

class EncounterController <Applicationcontoller
def index
end
end

And in the view I'm trying to access the first controller view in the second controller view as follows, but I got this error:

<%= render :partial => 'template_items/index', :locals => {:index=>@template_items} %>

And:

Missing partial template_items/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/murali/April22 ROR App/app/views" * "/var/lib/gems/1.9.1/gems/twitter-bootswatch-rails-helpers-2.3.1/app/views"

How can I fix this problem?

Upvotes: 1

Views: 466

Answers (3)

user2321302
user2321302

Reputation: 76

Make a file named template_items/_index.html.erb and in it write:

<%= render :file => 'template_items/index.html.erb' %>

Now use:

<%= render :partial => 'template_items/index', :locals => {:index=>@template_items} %>

It should work.

Upvotes: 1

shrikant1712
shrikant1712

Reputation: 4446

Actually, partials are saved as _index.html.erb, and in your views/template_items it is index.html.erb, so it says that partial is missing.

Upvotes: 0

sjain
sjain

Reputation: 23356

The error clearly says that you don't have _index partial in app/views/template_items.

Upvotes: 2

Related Questions