How to render a partials structure from another directory in Rails?

3

1) Views structure

Assuming I have two sets of views, set A and set B. Both sets are similar having the view index.html.erb and the partials _index.html.erb , _new.html.erb , _edit.html.erb , _table.html.erb , _show.html.erb , _bean.html.erb , _form.html.erb , and _field.html.erb .

2) Relationship between views

Both also have a similar format where index.html.erb view renders partial _index.html.erb ; The partial _index.html.erb renders partials _new.html.erb , _edit.html.erb , _table.html.erb , _show.html.erb ; Both partial _edit.html.erb and _new.html.erb render partial _form.html.erb ; The partial _form.html.erb redenizes the partial _fiels.html.erb ; And partials _show.html.erb and _table.html.erb rederize to partial _bean.html.erb .

3) Relationship between the two groups of views

But in my logic, object A has a one-to-many relationship with object B, and view a/index also renders partial b/_index .

4) Problem

When implementing the following code render partial: 'b/index' into a/index.html.erb the content of the b/_index.html.erb file is inserted correctly, however it renders the partials of the a folder and not the b folder. Example: a/_show.html.erb is rendered instead of b/_show.html.erb .

5) Doubt

But how can I determine the partial to be rendered, based on the partial directory that referenced it instead of the current view's directory?

    
asked by anonymous 21.10.2015 / 15:50

1 answer

2

You can put the entire path of the file, in case there is a partial inserted in app / views / users / _map.html.erb you can make the call so

<%= render 'users/map' %>

In case you need to specify an internal variable to the partial, just add the parameter

<%= render 'users/map', lat: @lat, lng: @lng %>

users / _map.html.erb

<div>
  <p>lat:<%= lat %></p>
  <p>lng:<%= lng %></p>
</div> 

If you need to specify a collection for a partial that has the model name ex: 'users / _user.html.erb'

<!--coloque o partial e especifique qual coleção de dados irá usar-->
<% render partial: 'users/user', collection: @guests %>
    
18.12.2015 / 13:23