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.
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