I have a controller show action which does some stuff and renders a view but due to some custom routing, I need a totally different action in a totally different controller to perform the same stuff and render the same view.
I don't really wish to duplicate the code. Is there somewhere I can put it and call it from both locations?
Edit:
I basically need to run the following from Collection#Show AND also from Gallery#SplitUrl:
@collection = Collection.find_by_id(params[:id])
if @collection.has_children?
@collections = @collection.children
else
redirect_to [@collection, :albums]
end
I cannot just redirect_to
Collection#Show at the end of Gallery#SplitUrl as the redirect causes my custom URL's to be lost as it's a new request.
You could put the view content into a partial
(/app/views/home/_example.html.erb)
and then use therender "example"
command in the HomeController.The "some stuff" code you talk about could be put into a helper file
/app/helpers/...
to save you from duplicating code in the two separate controllers although it could depend on what the code is trying to do in the first place.http://guides.rubyonrails.org/layouts_and_rendering.html
This might provide more information on the subject in general.