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

Asked

Viewed 532 times

3

1) Structure of views

Assuming I have two sets of views, the set A and the set B. Both sets are similar possessing 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) Relation between views

Both also have a similar format where the view index.html.erb render the partial _index.html.erb; A partial _index.html.erb render the partials _new.html.erb, _edit.html.erb, _table.html.erb, _show.html.erb; Both the partial _edit.html.erb as to _new.html.erb render a partial _form.html.erb; A partial _form.html.erb redempt the partial _fiels.html.erb; And the partials _show.html.erb and _table.html.erb rederizam a partial _bean.html.erb.

3) Relationship between the two view groups

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

4)Problem

By implementing the following code render partial: 'b/index' in a/index.html.erb the contents of the file b/_index.html.erb is inserted correctly, but the same renders the folder partitals a and no longer those of b. Example: is rendered a/_show.html.erb instead of b/_show.html.erb.

5) Doubt

However it has how I determine the partial to be rendered, based on the partial directory that referenced it in place of the current view directory?

  • It has to display the directory structure to be clearer?

1 answer

2

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

<%= render 'users/map' %>

and 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> 

and if you need to specify a colection 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 %>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.