Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering partial from another folder from another partial in Rails 3

For example: I'm have two models: Task and List. Task belongs_to List. I'm render lists/_form.html.erb partial within lists/show.html.erb view. Now I need to render tasks/_fields.html.erb partial within lists/_form.html.erb partial:

<%= render 'tasks/fields' %> 

But I get an error ActionView::MissingTemplate

If I try to render tasks/_fields.html.erb within lists/_form.html.erb, everything works.

I see two bad ways to solve this problem:

  • Place _fields.html.erb to lists folder
  • Make a view from lists/_form.html.erb partial and try a "Nested Layouts" from http://guides.rubyonrails.org/layouts_and_rendering.html

Is there a good way?

like image 263
aetaur Avatar asked Aug 19 '11 16:08

aetaur


People also ask

How do you render a partial?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html.

What is render partial in Ruby on Rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

What is the reason for having partials in Rails?

A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts. For example, you might have a login form that you want to display on 10 different pages on your site.


2 Answers

Try this:

<%= render :partial => 'tasks/fields' %> 
like image 135
Arun Kumar Arjunan Avatar answered Sep 25 '22 15:09

Arun Kumar Arjunan


If you are sharing things like this, why not put them into a folder like app/views/shared/ or directly into app/views/layouts?

like image 42
pdu Avatar answered Sep 24 '22 15:09

pdu