开发者

Rendering partial from another folder from another partial in Rails 3

开发者 https://www.devze.com 2023-03-29 14:21 出处:网络
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.

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?


Try this:

<%= render :partial => 'tasks/fields' %>


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


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


Based on @ArunKumarArjun but with the new Rails notation:

<%= render partial: 'tasks/fields' %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号