AJAX 请求返回缺少模板
我正在努力开发我的第一个基于 AJAX 的应用程序,需要帮助。我不断收到此错误消息
Template is missing
Missing template my_apps/refresh, application/refresh with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/csco/MyApps/app/views"
我有一个 Ruby on Rails 表单,其中有多个向下滚动菜单。我想在每次用户更改其选择时触发 AJAX 请求,以便实时更新表单的某些文本字段,而无需按按钮或重新加载整个页面。
这些是我的代码的相关部分:
新方法和编辑方法使用的部分表单 app/views/my_apps/_form.html.erb 摘录
<%= form_for(@my_app, :html => {:name => "MyAppInfo"}) do |f| %>
<%= f.label :"Program Administrator" %>
<select class="prog_man_class" id=Program_Manager onChange="this.form.get_results_button_name.click()">
<option value="1">Admin1</option>
<option value="2">Admin2</option>
<option value="3">Admin3</option>
</select>
// more select menus here
// some Ruby code here that ends by assigning a value to the field I want to be
// updated in real time, I called it Total Power ; Total Power value depends on
// the user's selection of Admin1, Admin2, or Admin3, as well as other selected options
<% @my_app.TotalPower=total_power %>
<%= f.label :"Total Power" %>
<%= f.text_field :TotalPower, :size => 6, :style => "text-align:right" %>
<div class="actions">
<%= f.submit "Get Results", :name => "get_results_button_name", :remote => "true" %>
</div>
<% end %>
app/controllers/my_app.rb
class MyAppController < ApplicationController
// other methods like index, show, new, edit, create, and destroy are here -->
# PUT /my_apps/1
# PUT /my_apps/1.json
def update
@my_app = MyApp.find(params[:id])
respond_to do |format|
if @my_app.update_attributes(params[:my_app])
format.html { render :action => "refresh", :notice => 'Update SUCCESSFUL!' }
format.js
else
format.html { render :action => "edit" }
format.json { render :json => @my_app.errors, :status => :unprocessable_entity }
end
end
end
end
app/views/my_apps/refresh.js.erb 摘录(我也尝试将其重命名为refresh.js.rjs,但也没有运气)
page.replace_html('TotalPower', render(@TotalPower))
任何帮助表示赞赏
I am struggling with my 1st AJAX based application and need help. I keep getting this error message
Template is missing
Missing template my_apps/refresh, application/refresh with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/csco/MyApps/app/views"
I have a Ruby on Rails form that has multiple scroll down menus. I want to trigger an AJAX request each time the user changes his selections so that some text fields of the form are updated in real time without need to press a button or reloading the whole page.
These are the relevant pieces of my code:
Excerpt of partial form app/views/my_apps/_form.html.erb used by new and edit methods
<%= form_for(@my_app, :html => {:name => "MyAppInfo"}) do |f| %>
<%= f.label :"Program Administrator" %>
<select class="prog_man_class" id=Program_Manager onChange="this.form.get_results_button_name.click()">
<option value="1">Admin1</option>
<option value="2">Admin2</option>
<option value="3">Admin3</option>
</select>
// more select menus here
// some Ruby code here that ends by assigning a value to the field I want to be
// updated in real time, I called it Total Power ; Total Power value depends on
// the user's selection of Admin1, Admin2, or Admin3, as well as other selected options
<% @my_app.TotalPower=total_power %>
<%= f.label :"Total Power" %>
<%= f.text_field :TotalPower, :size => 6, :style => "text-align:right" %>
<div class="actions">
<%= f.submit "Get Results", :name => "get_results_button_name", :remote => "true" %>
</div>
<% end %>
Excerpt of app/controllers/my_app.rb
class MyAppController < ApplicationController
// other methods like index, show, new, edit, create, and destroy are here -->
# PUT /my_apps/1
# PUT /my_apps/1.json
def update
@my_app = MyApp.find(params[:id])
respond_to do |format|
if @my_app.update_attributes(params[:my_app])
format.html { render :action => "refresh", :notice => 'Update SUCCESSFUL!' }
format.js
else
format.html { render :action => "edit" }
format.json { render :json => @my_app.errors, :status => :unprocessable_entity }
end
end
end
end
app/views/my_apps/refresh.js.erb (I also tried renaming it refresh.js.rjs with no luck either)
page.replace_html('TotalPower', render(@TotalPower))
Any help is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有 html 格式的
app/views/my_apps/refresh.html.erb
文件吗?这似乎是问题所在。Have you got a
app/views/my_apps/refresh.html.erb
file for the html format? This seems to be the problem.我简单地通过恢复 format.html 和 format.js 的顺序来解决我的问题,如下所示。我不知道这是否是正确的方法,但它对我有用,而且我不再看到丢失模板的错误。
I fixed my problem simply by reverting the order of format.html and format.js like below. I don't know if it's the proper way but it worked for me and I don't see the missing template error anymore.
这里的问题是您的表单正在发出正常请求,而不是 ajax 请求,这就是它尝试加载
refresh.html
而不是refresh.js
的原因。为了解决这个问题,您需要在表单中包含远程选项,如下所示,我认为:不完全知道它去哪里,在那里或在 de html 选项哈希内尝试它,直到您在页面的源代码中看到该表单具有 de 选项
data-remote="true"
。The problem here is that your form is making a normal request and not an ajax request that's why it was trying to load
refresh.html
insted of yourrefresh.js
. To solve that you need to include the remote option in your form like this i think:Don't exactly know where it goes, there or inside de html options hash try it until you see in your page's source coda that the form has de option
data-remote="true"
.