Rendering partial from another folder from another partial in Rails 3
Asked Answered
P

4

30

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:

Is there a good way?

Pasahow answered 19/8, 2011 at 16:44 Comment(0)
S
46

Try this:

<%= render :partial => 'tasks/fields' %>
Scherle answered 19/8, 2011 at 16:49 Comment(0)
D
7

For Rails 5 & above:

You better use the render without partial like this:

<% render 'tasks/fields' %>

Because the partial can cause this kind of issues & is not needed any more

Disagreeable answered 16/9, 2019 at 12:43 Comment(0)
B
6

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

Barroom answered 19/8, 2011 at 16:48 Comment(1)
Because it logically belongs to Task modelPasahow
C
5

Based on @ArunKumarArjun but with the new Rails notation:

<%= render partial: 'tasks/fields' %>
Crafty answered 20/12, 2019 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.