Reputation: 1457
I'm trying to get AJAX working on my app and am getting a strange ActionView::MissingTemplate
error. I have this as my tasks#create
method:
def create
@task = Task.new(task_params)
@task.user_id = current_user.id
if @task.save
respond_to do |format|
format.js
format.html
end
# redirect_to tasks_path, notice: 'Task was successfully created.'
else
render :new
end
end
This as my views/tasks/create.js.erb
:
$("#onetime-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @one_time }) %>")
$("#onetime-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @one_time_done }) %>")
$("#daily-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @daily }) %>")
$("#daily-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @daily_done }) %>")
$("#weekly-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @weekly }) %>")
$("#weekly-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @weekly_done }) %>")
$("#monthly-todo").html("<%= escape_javascript(render partial: 'items', locals: { task: @monthly }) %>")
$("#monthly-done").html("<%= escape_javascript(render partial: 'done', locals: { task: @monthly_done }) %>")
And this on my tasks/index.html.erb
:
<%= simple_form_for(@create_task) do |f| %>
<div class="row">
<div class="container">
<div class="col-xs-12 col-sm-9">
<%= f.text_field :name, placeholder: "What needs to get done?", class: "form-control" %>
</div>
<div class="col-xs-12 col-sm-3">
<%= f.select :frequency, options_for_select([["One-Time", "OneTime"],["Daily", "Daily"],["Weekly", "Weekly"],["Monthly", "Monthly"]]), {}, {class: "form-control"} %>
</div>
<div class="col-xs-12 text-center">
<%= f.button :submit, class: "btn ghost", remote: true %>
</div>
</div> <!-- container -->
</div> <!-- row -->
<% end %>
....
<div class="col-xs-12 col-md-6 col-lg-12">
<div class="content-box border-green">
<h1>One-Time Tasks</h1>
<div id="onetime-todo"><%= render partial: 'items', locals: { task: @one_time } %></div>
<div id="onetime-done"><%= render partial: 'done', locals: { task: @one_time_done } %></div>
</div> <!-- content box -->
</div> <!-- col -->
<div class="col-xs-12 col-md-6 col-lg-4">
<div class="content-box">
<h1>Daily</h1>
<div id="daily-todo"><%= render partial: 'items', locals: { task: @daily } %></div>
<div id="daily-done"><%= render partial: 'done', locals: { task: @daily_done } %></div>
</div> <!-- content box -->
</div> <!-- col -->
<div class="col-xs-12 col-md-6 col-lg-4">
<div class="content-box">
<h1>Weekly</h1>
<div id="weekly-todo"><%= render partial: 'items', locals: { task: @weekly } %></div>
<div id="weekly-done"><%= render partial: 'done', locals: { task: @weekly_done } %></div>
</div> <!-- content box -->
</div> <!-- col -->
<div class="col-xs-12 col-md-6 col-lg-4">
<div class="content-box">
<h1>Montly</h1>
<div id="monthly-todo"><%= render partial: 'items', locals: { task: @monthly } %></div>
<div id="monthly-done"><%= render partial: 'done', locals: { task: @monthly_done } %></div>
</div> <!-- content box -->
</div> <!-- col -->
Can anyone see where I'm going wrong? It seems like a weird error because I do have a create template. My server log shows this:
Started POST "/tasks" for ::1 at 2017-08-13 21:48:44 -0700
Processing by TasksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"guwmQBf1AmKhxvi1coysoegbkKhy6Znk/oMKEar4TXcZ1OieTqiyRnf3m3MR/XHBrOsJ1d7ncZp8pzqAxC93tQ==", "task"=>{"name"=>"Testing oh testing", "frequency"=>"OneTime"}, "commit"=>"Create Task"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "tasks" ("name", "frequency", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["name", "Testing oh testing"], ["frequency", "OneTime"], ["user_id", 1], ["created_at", "2017-08-14 04:48:44.237114"], ["updated_at", "2017-08-14 04:48:44.237114"]]
(2.0ms) commit transaction
Completed 500 Internal Server Error in 13ms (ActiveRecord: 2.6ms)
ActionView::MissingTemplate - Missing template tasks/create, application/create with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in:
* "/Users/lizbayardelle/Dropbox/Code/FAM/app/views"
* "/Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/ckeditor-4.2.2/app/views"
* "/Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/devise-4.2.0/app/views"
And my routes.rb
are very simple, if it helps:
resources :tasks
Can anyone help here?
ADDITIONAL INFO
As requested, here is my tasks#index
method:
def index
@create_task = Task.new
@tasks = Task.all
@one_time = Task.where(frequency: "OneTime", completed: false, user_id: current_user.id)
@one_time_done = Task.where(frequency: "OneTime", completed: true, user_id: current_user.id)
@daily = Task.where(frequency: "Daily", completed: false, user_id: current_user.id)
@daily_done = Task.where(frequency: "Daily", completed: true, user_id: current_user.id)
@weekly = Task.where(frequency: "Weekly", completed: false, user_id: current_user.id)
@weekly_done = Task.where(frequency: "Weekly", completed: true, user_id: current_user.id)
@monthly = Task.where(frequency: "Monthly", completed: false, user_id: current_user.id)
@monthly_done = Task.where(frequency: "Monthly", completed: true, user_id: current_user.id)
end
Upvotes: 0
Views: 793
Reputation: 10507
I'm trying to get AJAX working on my app and am getting a strange
ActionView::MissingTemplate
error.
The request is being made as HTML, that is, the remote: true
in the submit button
is not working, try setting it in the form
tag, like this:
<%= simple_form_for(@create_task), remote: true do |f| %>
I'm getting this error in my server:
Completed 500 Internal Server Error in 28ms (ActiveRecord: 1.3ms) NoMethodError - undefined method each' for nil:NilClass: app/views/tasks/_items.html.erb:2:in _app_views_tasks__items_html_erb__1746796241772610323_70298103116960'
The problem is that all variables that are required in create.js.erb have not being set in create
action, they are only set in index
. So you need to create all those variables also in create
, but you can avoid repetition using a before_action
callback, like this:
class TasksController < ApplicationController
before_action :set_variables, only: [:index, :create]
def index
end
def create
@task = Task.new(task_params)
@task.user_id = current_user.id
if @task.save
respond_to do |format|
format.js
format.html
end
# redirect_to tasks_path, notice: 'Task was successfully created.'
else
render :new
end
end
# other actions
private
def set_variables
@create_task = Task.new
@tasks = Task.all
@one_time = Task.where(frequency: "OneTime", completed: false, user_id: current_user.id)
@one_time_done = Task.where(frequency: "OneTime", completed: true, user_id: current_user.id)
@daily = Task.where(frequency: "Daily", completed: false, user_id: current_user.id)
@daily_done = Task.where(frequency: "Daily", completed: true, user_id: current_user.id)
@weekly = Task.where(frequency: "Weekly", completed: false, user_id: current_user.id)
@weekly_done = Task.where(frequency: "Weekly", completed: true, user_id: current_user.id)
@monthly = Task.where(frequency: "Monthly", completed: false, user_id: current_user.id)
@monthly_done = Task.where(frequency: "Monthly", completed: true, user_id: current_user.id)
end
end
Upvotes: 1