简体   繁体   中英

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?

该错误清楚地表明您在app/views/template_items没有_index partial。

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.

实际上,partials保存为_index.html.erb ,在views / template_items中它是index.html.erb ,因此它表示partial缺失。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM