ActionController UnknownFormat Error in rails 4

ActionController UnknownFormat Error in rails 4

本文关键字:rails in Error UnknownFormat ActionController      更新时间:2023-09-26

我在rails中有一个问题。

  1. 视图(index.html.erb)

    <div id="container" style="margin:0 auto;">
      <div style="float:left; height: 300px; width: 300px" align="center">
        <%= form_tag("/java/select", :method => "post", :remote => true) do %>
          Deivce1 <%= radio_button_tag "devices", "1"%><br>
          Deivce2 <%= radio_button_tag "devices", "2"%><br>
          Deivce3 <%= radio_button_tag "devices", "3"%><br>
          <%= submit_tag "On/Off" %>
        <% end %>
      </div>
    </div>
    <div id="statelist"></div>
    
  2. 控制器(java_controller.rb)

    class JavaController < ApplicationController
    skip_before_filter :verify_authenticity_token
        def select
          @device = params[:devices]
          respond_to do|format|
             format.js
          end
        end
    end
    
  3. js文件(select.js.erb)

    $('#statelist').empty().append('<%= j render 'statelist'%>');
    
  4. html文件(_statelist.html.erb)

    Selected device is <%= @device %>
    
  5. 根文件(rootes.rb)

    root "java#index"
    post "java/select" => "java#select"
    

我想我有所有的组件来操作ajax。此外,我通常提前使用类似的语法。

然而,在这种情况下,当我点击"开/关"按钮时,网页上出现了未知格式的错误信息。

此错误指示"respond_to do |format|"行在控制器。

有什么问题吗?谢谢您的关注。


    日志…
  • Started POST "/java/select" for 127.0.0.1 at 2014-08-14 17:48:55 +0900
    Processing by JavaController#select as HTML
      Parameters: {"utf8"=>"✓", "devices"=>"2", "commit"=>"On/Off"}
    Completed 406 Not Acceptable in 6ms
    ActionController::UnknownFormat (ActionController::UnknownFormat):
      app/controllers/java_controller.rb:14:in `select'
    
      Rendered /usr/lib/jruby/jruby-1.7.13/lib/ruby/gems/shared/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.0ms)
      Rendered /usr/lib/jruby/jruby-1.7.13/lib/ruby/gems/shared/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (14.0ms)
      Rendered /usr/lib/jruby/jruby-1.7.13/lib/ruby/gems/shared/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (19.0ms)
      Rendered /usr/lib/jruby/jruby-1.7.13/lib/ruby/gems/shared/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (291.0ms)
    

直接删除

 respond_to do|format|
     format.js
  end

块。在您的示例中,它将根据rails默认值自动加载select.js部分。我不确定,但也许您需要将路径更改为

/java/select.js