Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
Action View missing Template in RoR 5 when calling instance variable from controller

Trying to render a to-do list with Ajax through Ruby on Rails. Here is the index.html.erb snippet:

<div class="row">
  <div class="col-md-7 col-md-offset-1" id="tasks"><%= render @tasks %></div>
</div>

The error raises when render @tasks hits. The message states "Missing partial tasks/_task "

My controller declares that @tasks = Task.all as follows

class TasksController < ApplicationController
before_action :all_tasks, only: [:index, :create]
respond_to :html, :js

def index
    @tasks = Task.all
end

def new
    @task = Task.new
end

def create
    @task = Task.create(task_params)
end

private

    def all_tasks
        @tasks = Task.all
    end

    def task_params
        params.require(:task).permit(:description, :deadline)
    end
end

Not sure what the issue is in this situation.

Any help appreciated.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's asking for a task partial within your app/views/tasks folder.

To use or print @tasks you just call use it in your view using Ruby inside your views like <%= ruby_code %>

By default, controllers in Rails automatically render views with names that correspond to actions, it means if you use <%= render @tasks %>, Rails will try to find for some partial called tasks within the parent folder that's printing the current view.

Also I've seen you're assigning twice the value of @books in your index method, if you're using the before_action :all_tasks in your index then you don't need to "redeclare" it again.

Try with:

# app/views/index.html.erb
<div class="row">
  <div class="col-md-7 col-md-offset-1" id="tasks">
    <%= @tasks %>
  </div>
</div>

# app/controllers/tasks_controller.rb
class TasksController < ApplicationController
  before_action :all_tasks, only: [:index, :create]
  respond_to :html, :js

  def index
  end

  def new
    @task = Task.new
  end

  def create
    @task = Task.create(task_params)
  end

  private

  def all_tasks
    @tasks = Task.all
  end

  def task_params
    params.require(:task).permit(:description, :deadline)
  end
end
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda