我在rails中遇到了问题。
查看(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>
控制器(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
js文件(select.js.erb)
$('#statelist').empty().append('<%= j render 'statelist'%>');
html文件(_statelist.html.erb)
Selected device is <%= @device %>
根文件(rootes.rb)
root "java#index"
post "java/select" => "java#select"
我认为我拥有操作ajax的所有组件。另外,我通常提前使用类似的语法。
然而,在这种情况下,当我点击&#34;开/关&#34;按钮,网页中出现Unknownformat错误消息。
此错误表示&#34; respond_to do | format |&#34;控制器线。
问题是什么?谢谢你的关注。
日志...
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)
答案 0 :(得分:0)
只需删除
即可 respond_to do|format|
format.js
end
块。在您的情况下,它将根据rails默认值自动加载select.js部分。我不确定,但也许你需要改变路径
/java/select.js
也