Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: render template looking for the wrong partial

I have a TasksController, and a SubtasksController. In a given moment of an action from SubtasksController I want to:

# app/controllers/tasks_controllers.rb
render 'tasks/index' # Or: render template: 'tasks/index'

When that action is called from the view though, it appears rails is trying to render the wrong partial:

ActionView::Template::Error (Missing partial subtasks/tasks, private_area/tasks, application/tasks with {:locale=>[:ca, :es], :formats=>[:js, :html], :handlers=>[:erb, :builder, :slim, :jbuilder, :coffee, :haml]}. Searched in:
  * "/Users/****/app/views"

I really don't understand what's going on here, any thoughts?

like image 677
Genís Avatar asked Nov 25 '25 01:11

Genís


1 Answers

When you render another controllers action the page that is displayed will look in the calling controllers views for any partials within itself.

You can get around this by explicitly declaring the path to partials within the page so that even when called from another controller it will always look in the right place.

# tasks/index page 
<%= render 'tasks/some_partial' %>

Now no matter which controller renders this page it will always look in tasks for its partials.

like image 106
Matt Avatar answered Nov 26 '25 17:11

Matt