I have both task and request controllers and models respectively. The new action of request depends on a task :id which is included in the route. If the request cannot be save due to a validation error, I need to be able to render the new action of the request controller with the task :id. However, when the code below runs, I get a template is missing error.
How can I render the new template with the task :id as a param?
/task/1/request/new
if !@request.save
render :template => new_task_request_path(@blog) # /task/1/request/new
end
—–> Error: Template is missing –
Missing template blogs/1/requests/new
In your code,
new_task_request_path(@blog)generates a relative URL.When rendering a template you need to give it the path to the template, not the relative URL.
I can’t tell where your view template is going to be without more information, but try changing your code in the
createaction to the following:You can then access the Task ID using
@task.idin your view.This assumes that
Taskhas manyRequests, and that this action is in yourRequestsController. If you’re using a different relationship you’ll need to update the controller action code as necessary. Similarly, if it’s a different controller you should update the template path to be something like'requests/new'.