Pass objects collection via link_to

1

Good morning, how could a collection of objects pass through link_to between views in rails?

<%= link_to '<i class="fa fa-arrow-right" aria-hidden="true"></i>'.html_safe, family_group_path(obj), data: {modal: true} %> 
    
asked by sandovalgus 20.03.2017 в 14:07
source

1 answer

1

A simple way to send an object from one view to another view is to use the controller to receive the object in the hash params[] , and then assign it to an instance variable.

The first thing is to send the object from the source view, assign it a variable obj , and then pass it as a parameter this object in the form of a hash to the helper path

#view1.html.erb
<% object = coleccion_objetos %> 
<%= link_to 'texto', family_group_path(obj, :send_object => object) %> 

This makes it available in the controller in params[:send_object] , so it can be assigned to a variable of instance

#family_group_controller.rb
def show
    ...
    @send_object = params[:send_object]
end

And finally, in the target view you can use the instance variable

#view2.html.erb
<h2>@send_object: <%= @send_object %> </h2>
    
answered by 20.03.2017 / 17:59
source