所以我在rails中使用非常基本的ajax代码,在我的/ demo视图中我有这个
<%= form_for(:session, url: demo_sample_email_path, remote: true) do |f| %>
并重定向到此方法
def demo_sample_email
//do stuff
respond_to do |format|
format.html { redirect_to demo_path }
format.js
end
end
根据我对Ajax的理解,/ demo页面不应该刷新,但我发现它确实如此,因为页面中的所有内容都会刷新。我插入了&#34; remote:true&#34;这应该不会让它刷新。
对于我的应用程序,我只想做一个POST
表单ajax请求,它将信息发送到服务器,而不是任何回到浏览器。
Started POST "/demo_sample_email" for 127.0.0.1 at 2015-06-16 01:14:55 -0700
Processing by StaticPagesController#demo_sample_email as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"A60kCDxuItxBanhv02uXIST8kpFdNtWceVQRw13cTE7s/fzo7Z137SxQRxfNpwpAzmUg1lyEm2UD75U5bJeGXA==", "session"=>{"email"=>"blah@blah.com"}, "commit"=>"Send me emails now"}
Redirected to http://localhost:3000/demo
Completed 302 Found in 79ms (ActiveRecord: 0.0ms)
Started GET "/demo" for 127.0.0.1 at 2015-06-16 01:14:55 -0700
Processing by StaticPagesController#demo as HTML
Rendered static_pages/demo.html.erb within layouts/application (85.5ms)
Rendered layouts/_shim.html.erb (2.6ms)
User Load (14.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 3]]
Rendered layouts/_header.html.erb (63.5ms)
Completed 200 OK in 533ms (Views: 510.4ms | ActiveRecord: 14.6ms)
Started GET "/javascripts/application.js" for 127.0.0.1 at 2015-06-16 01:14:57 -0700
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.1) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.1) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.1) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.1) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.1) lib/rack/content_length.rb:15:in `call'
thin (1.6.3) lib/thin/connection.rb:86:in `block in pre_process'
thin (1.6.3) lib/thin/connection.rb:84:in `catch'
thin (1.6.3) lib/thin/connection.rb:84:in `pre_process'
thin (1.6.3) lib/thin/connection.rb:53:in `process'
thin (1.6.3) lib/thin/connection.rb:39:in `receive_data'
eventmachine (1.0.7) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.7) lib/eventmachine.rb:187:in `run'
thin (1.6.3) lib/thin/backends/base.rb:73:in `start'
thin (1.6.3) lib/thin/server.rb:162:in `start'
rack (1.6.1) lib/rack/handler/thin.rb:19:in `run'
rack (1.6.1) lib/rack/server.rb:286:in `start'
railties (4.2.0) lib/rails/commands/server.rb:80:in `start'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:80:in `block in server'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:75:in `tap'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:75:in `server'
railties (4.2.0) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
railties (4.2.0) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:8:in `require'
bin/rails:8:in `<top (required)>'
spring (1.1.3) lib/spring/client/rails.rb:27:in `load'
spring (1.1.3) lib/spring/client/rails.rb:27:in `call'
spring (1.1.3) lib/spring/client/command.rb:7:in `call'
spring (1.1.3) lib/spring/client.rb:26:in `run'
spring (1.1.3) bin/spring:48:in `<top (required)>'
spring (1.1.3) lib/spring/binstub.rb:11:in `load'
spring (1.1.3) lib/spring/binstub.rb:11:in `<top (required)>'
bin/spring:13:in `require'
bin/spring:13:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
答案 0 :(得分:0)
您可以尝试render partial: "your/view", layout: false, locals: {:@foo => @bar}
如果你指示一个div来返回ajax响应,它将返回一个视图。
您还可以渲染json format.json { render :json => {:success => true, :html => (render_to_string 'your/view')} }
希望有所帮助
答案 1 :(得分:0)
如果您的优先级为js call且 NOT HTML调用,则将其更改为..
respond_to do |format|
##added .js first and then html,call will come for js,if not then html
format.js
format.html { redirect_to demo_path }
end
希望它有所帮助。