AJAX 请求返回缺少模板

发布于 2024-12-22 23:02:26 字数 2350 浏览 0 评论 0原文

我正在努力开发我的第一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强者自强 2024-12-29 23:02:26

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

幸福不弃 2024-12-29 23:02:26

我简单地通过恢复 format.html 和 format.js 的顺序来解决我的问题,如下所示。我不知道这是否是正确的方法,但它对我有用,而且我不再看到丢失模板的错误。

respond_to do |format|
  if @my_app.update_attributes(params[:my_app])
    format.js
    format.html { render :action => "refresh", :notice => 'Update SUCCESSFUL!' }
  else
    format.html { render :action => "edit" }
    format.json { render :json => @my_app.errors, :status => :unprocessable_entity }
  end
end

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.

respond_to do |format|
  if @my_app.update_attributes(params[:my_app])
    format.js
    format.html { render :action => "refresh", :notice => 'Update SUCCESSFUL!' }
  else
    format.html { render :action => "edit" }
    format.json { render :json => @my_app.errors, :status => :unprocessable_entity }
  end
end
七分※倦醒 2024-12-29 23:02:26

这里的问题是您的表单正在发出正常请求,而不是 ajax 请求,这就是它尝试加载 refresh.html 而不是 refresh.js 的原因。为了解决这个问题,您需要在表单中包含远程选项,如下所示,我认为:

<%= form_for(@my_app, :remote => true, :html => {:name => "MyAppInfo"}) do |f| %>

不完全知道它去哪里,在那里或在 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 your refresh.js. To solve that you need to include the remote option in your form like this i think:

<%= form_for(@my_app, :remote => true, :html => {:name => "MyAppInfo"}) do |f| %>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文