You can commit a variable to a partial using the :object
or the :collection
options in the render
method. The :object
option passes a single object to the partial that can contain anything. The :collection
option is used to pass an Array of Objects that must be from the same Class
.
For example you have your @post and pass it to the partial like this
<%= render :partial => 'layouts/my_partial', :object => @post, :as => :my_local_var %>
Then you can access the object by calling my_local_var
in the partial. Using :collection
is slightly different but you can read it on your own here: http://guides.rubyonrails.org/layouts_and_rendering.html
A Problem will be the usage of one partial in different Classes because of the routes names. Then you cant use the edit_modelname_path(@instance_var)
you must use the routes Hash { :controller => params[:controller], :action => 'edit' }
in the link_to
method!
// For Link creation wihout params[:controller]
:
Its just an Idea I didnt do this before but you can try this for example:
You render the partial and use :object => @instancevar, :as => :my_local_var
you can use the following line for dynamic link creation:
<%= link_to "link text", { :controller => my_local_var.class.to_s.pluralize.downcase, :action => 'edit', :id => my_local_var.id } %>
Then you dont depend on the params[:controller]
but are able to link to the vars edit link.