我试图让AJAX在我的应用上运行,并且出现了一个奇怪的ActionView::MissingTemplate
错误。我将此作为tasks#create
方法:
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
这是我的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 }) %>")
这是我的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 -->
任何人都可以看到我出错的地方吗?这似乎是一个奇怪的错误,因为我做有一个创建模板。我的服务器日志显示了这个:
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"
我的routes.rb
非常简单,如果有帮助的话:
资源:任务
有人可以帮忙吗?
附加信息
根据要求,这是我的tasks#index
方法:
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
答案 0 :(得分:1)
我正在尝试让AJAX在我的应用上工作,并且变得奇怪了
ActionView::MissingTemplate
错误。
请求以 HTML 形式发出,即提交remote: true
中的button
无效,请尝试在form
标记中进行设置,像这样:
<%= simple_form_for(@create_task), remote: true do |f| %>
我在服务器中收到此错误:
在28ms内完成500内部服务器错误(ActiveRecord:1.3ms) NoMethodError - 每个'未定义的方法'为nil:NilClass: 应用程序/视图/任务/ _items.html.erb:2: _app_views_tasks__items_html_erb__1746796241772610323_702981 03116960'
问题是 create.js.erb 中所需的所有变量都未在create
操作中设置,它们仅在index
中设置。所以你需要在create
中创建所有这些变量,但你可以避免使用before_action
回调重复,如下所示:
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