【问题标题】:Rails 4 - Template is missingRails 4 - 模板丢失
【发布时间】:2014-05-09 16:17:59
【问题描述】:

希望您能帮我弄清楚为什么会出现这个奇怪的错误。

我有以下表格:

index.html.erb

<%= form_for :response, :url => {:action => 'create'}, :remote => true do |f| %> 
  <%= f.hidden_field :template_id, :value => @template.id %>
    <%= button_tag(type: 'submit', class: "btn btn-primary btn-lg pull-right next-slide") do %>
      Next &rarr;
     <% end %>
<% end %>

therapy_controller.erb

 def create
   @response = Response.new(response_params)
    respond_to do |format| 
      if @result = @response.save 
        format.html 
        format.js  
       else  
        format.html { render :action => "new" }  
        format.js
       end  
     end
   end

views/therapy/create.js.erb

<% if @result %>
  alert("Success!");
<% else %>
  alert("Fail!");
<% end %>

但是,当我提交表单时,我收到以下错误:

模板丢失:

缺少模板治疗/创建,应用程序/创建与 {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder, :slim]}。搜索:*...

任何想法为什么我会收到此错误?我不知道是什么原因造成的。

谢谢!

更新:

所以,我已经尝试了 MrYoshiji、Dave 和 Pavan 的建议,但我仍然遇到同样的错误。所以,在我的therapy_controller.erb,我把它改成了:

respond_to do |format|
  format.js
end

现在,我得到了错误:

ActionController::UnknownFormat in TherapyController#create 

有什么想法吗?

更新 2:以下是服务器日志:

 Started POST "/therapy" for 152.79.45.121 at 2014-05-15 18:01:21 +0000
 Processing by TherapyController#create as HTML
  Parameters: {"utf8"=>"✓", "response"=>{"template_id"=>"2"}, "commit"=>"Save Response"}
 Completed 406 Not Acceptable in 1ms

  ActionController::UnknownFormat (ActionController::UnknownFormat):
    app/controllers/therapy_controller.rb:13:in `create'

routes.rb 文件在这里:https://gist.github.com/dodinas/fb0a39282022e961ce09

谢谢。

【问题讨论】:

  • ajax 请求应该呈现 js.erb 而不是 html.erb。看来你错过了什么
  • 检查此链接是否有帮助 blog.markhorgan.com/?p=522
  • 试试这个format.js { render 'create.js.erb' }
  • 我的回答对你有用吗?
  • @Dodinas 重定向到“therapy.js”?? AFAIK,这里没有您提到的重定向代码。您必须还原所做的所有更改。只需从您的问题中更改“index.html.erb”并保留您在问题中提到的所有内容。它应该工作!

标签: ruby-on-rails ajax missing-template


【解决方案1】:

试试这个

def create
  @response = Response.new(response_params)
  respond_to do |format| 
    if @result = @response.save 
      format.html 
      format.js { render 'create.js.erb' }
    else  
      format.html { render :action => "new" }  
      format.js
    end  
  end
end

Source

【讨论】:

    【解决方案2】:

    错误来自浏览器请求 HTML 数据和控制器只接受 js。我建议在 index.html.erb 中将:format =&gt; :js 添加到form_for 的选项中:

    <%= form_for :response, :url => {:action => 'create', :format => :js}, :remote => true do |f| %>
    

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      相关资源
      最近更新 更多