简体   繁体   English

Ruby on Rails-AJAX问题(ActionController:UnknownFormat)

[英]Ruby on Rails - Issues with AJAX (ActionController:UnknownFormat)

I am a beginner with Ruby on Rails so I apologize if the issue is obvious. 我是Ruby on Rails的初学者,所以如果问题很明显,我深表歉意。 I am currently working on a project called Gradebook. 我目前正在从事一个名为Gradebook的项目。 On one page in Gradebook, I've been working on a feature where a user will be able to click "New Category", and a form will be rendered within a div that will allow the user to enter information about the category, and then the category will show on the screen once the changes are saved. 在Gradebook的一页上,我一直在研究一种功能,用户可以单击“新建类别”,并且将在div中呈现一个表单,该表单将允许用户输入有关类别的信息,然后保存更改后,类别将显示在屏幕上。 All of this would occur using AJAX. 所有这些都将使用AJAX发生。

Here is my thought process: 这是我的思考过程:

  1. I add a submit button to the view that will trigger the "new" action on the Category controller. 我向视图添加一个提交按钮,该按钮将触发类别控制器上的“新”操作。 I use remote: true so the page does not redirect to /category/new. 我使用remote:true,因此页面不会重定向到/ category / new。 I create a div towards the bottom of the form in which the category form will be rendered. 我在将要呈现类别表单的表单底部创建一个div。

     ... <%= form_for :category, url: new_gradebook_category_path, remote: true do |f| %> <%= f.submit 'New Category' %> <% end %> ... <div id="category_form"></div> 
  2. In the category controller, I create a new category and specify that I only want to produce a javascript response: 在类别控制器中,我创建一个新类别并指定我只想产生一个javascript响应:

     def new @category = Category.new respond_to do |format| format.js end end 
  3. I create "new.js.erb" in the category folder that will be responsible for rendering my category form within the div: 我在类别文件夹中创建“ new.js.erb”,该文件夹将负责在div中呈现我的类别表单:

     $('#category_form').html("<%= j (render 'newcategory') %>"); 
  4. I create _newcategory.html.erb that is the actual form: 我创建了_newcategory.html.erb,它是实际的形式:

     <%= form_for :category, url: gradebook_category_index_path, remote: true do |f| %> <p> <%= f.label :category_name, "Category Name"%><br> <%= f.text_field :category_name %> </p> <p> <%= f.label :category_description, "Category Description"%><br> <%= f.text_area :category_description %> </p> <p> <%= f.submit, "Create Category" %> </p> <% end %> 

My issue is, when the user clicks on the "New Category" button, nothing happens. 我的问题是,当用户单击“新建类别”按钮时,什么也没有发生。 I looked on the Javascript Console and noticed that "new.js.erb" is not being found: 我查看了Javascript控制台,发现找不到“ new.js.erb”:

在此处输入图片说明在此处输入图片说明

When I go to open up this link in a new tab, I get ActionController:UnknownFormat: 当我在新选项卡中打开此链接时,我得到了ActionController:UnknownFormat:

在此处输入图片说明

Here is all the things that I have tried: 这是我尝试过的所有方法:

  1. I tried changing: 我尝试更改:

      respond_to do |format| format.js end 

    to this: 对此:

      respond_to do |format| format.html format.js end 

    I have read that using "format.js" as the only response can produce this error. 我已经读过使用“ format.js”作为唯一响应会产生此错误。 However this just gives me "missing template category/new". 但是,这只是给我“缺少模板类别/新”。 Although I shouldn't have to specify new.html.erb, because I want this to be an AJAX response and I would like it to stay on the same page. 尽管我不必指定new.html.erb,但因为我希望它是AJAX响应,所以我希望它保留在同一页面上。

  2. I have tried deleting the tmp folder, but this did not work. 我曾尝试删除tmp文件夹,但这没有用。

  3. I checked to make sure the file exists using "File.exists?", they did exist. 我使用“ File.exists?”检查文件是否存在,它们确实存在。

  4. I checked to make sure the server wasn't interpreting it as HTML, and according to my server output: 我检查以确保服务器没有将其解释为HTML,并根据服务器输出:

      Started POST "/gradebook/2/category/new" for 127.0.0.1 at 2014-10-16 16:43:02 -0700 Processing by ApplicationController#routing_error as JS Parameters: {"utf8"=>"✓", "commit"=>"New Category", "path"=>"gradebook/2/category/new"} Rendered public/404.html (0.1ms) Completed 404 Not Found in 12ms (Views: 11.3ms | ActiveRecord: 0.0ms) 

    It is being interpreted as Javascript file, however it does show "ApplicationController#routing_error", but I fail to understand where this routing error is occuring. 它被解释为Javascript文件,但是确实显示“ ApplicationController#routing_error”,但是我无法理解此路由错误发生的位置。

    1. I checked to make sure JQuery was enabled (as the JS file uses JQuery), and it is enabled. 我检查以确保启用了JQuery(因为JS文件使用JQuery)并且已启用。

At this point, I'm not really sure what the issue is. 在这一点上,我不确定是什么问题。 I feel like the issue is super obvious, but the rails server is acting like the javascript file does not even exist. 我觉得这个问题非常明显,但是Rails服务器的行为就像是javascript文件都不存在。 Any help would be much appreciated. 任何帮助将非常感激。 Thanks a lot. 非常感谢。

yes, it's because if you have a look in your screenshots you will see that is action POST ( form_for automatically uses POST method), but rails handle requests with 'new' like GET. 是的,这是因为如果您在屏幕快照中查看,您会看到这是动作POST( form_for自动使用POST方法),但是rails处理带有“新”(如GET)的请求。 If you type rake routes or bundle exec rake routes you will find your action as GET. 如果您输入rake routesbundle exec rake routes您将发现您的操作为GET。 You can set directly in form to be action GET instead of POST: 您可以直接在表单中将其设置为操作GET而不是POST:

form_for :category, url: new_gradebook_category_path, method: :get, remote: true do |f|

I hope it helped 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 6.0.1 中 Ajax 请求的 ActionController::UnknownFormat 错误 - ActionController::UnknownFormat error with Ajax request in Rails 6.0.1 Rails中的ActionController UnknownFormat错误4 - ActionController UnknownFormat Error in rails 4 Rails Form_Tag Ajax Format.js ActionController :: UnknownFormat - Rails Form_Tag Ajax Format.js ActionController::UnknownFormat 带AJAX的response_to js的ActionController :: UnknownFormat - ActionController::UnknownFormat with respond_to js for AJAX ActionController::UnknownFormat in CareersController#create for rails 4 - ActionController::UnknownFormat in CareersController#create for rails 4 Rails respond_to 抛出 ActionController::UnknownFormat - Rails respond_to throw ActionController::UnknownFormat Rails 4 ActionController :: UnknownFormat format.js错误 - Rails 4 ActionController::UnknownFormat format.js error ActionController::UnknownFormat 尝试将 .js 文件添加到我的 Rails 应用程序时 - ActionController::UnknownFormat when try to add .js file to my rails app Rails,“ respond_to do | format |”返回ActionController :: UnknownFormat - Rails, “respond_to do |format|” returns ActionController::UnknownFormat ActionController :: UnknownFormat当使用response_来做| format |时 在Rails 5应用中 - ActionController::UnknownFormat When using respond_to do |format| in rails 5 app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM