本文介绍了关于简单的"Hello World" Rails-Ajax示例的想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Rails学习/重新学习Ajax.我认为rails-ajax在最近几年发生了很大变化,因此没有找到一个非常简单的"Hello World"类型的ajax示例(没有资源,没有记录).

Learning/re-learning ajax using rails. I think rails-ajax has changed a lot in last few years so have not found a very simple 'Hello World' type ajax example (w/o resources, w/o records).

这是基于我能找到的引用的简单Rails Ajax示例的尝试(下).它可以工作-但是,看起来有点麻烦(即,一对一的.js文件到ajax请求操作).有人对改进的(更优雅的)Rails Ajax"Hello World"有想法吗?

This is an attempt (below) at a simple rails ajax example based on references I could find.It works -- however, it seems a bit cumbersome (ie one-to-one .js file to ajax request action). Does anyone have thoughts on an improved (more elegant) rails ajax 'Hello World'?

---------------------
View - 'edit.html.erb'


 <div id="div_msg">
 <%= link_to "Wow",
  {:controller=>"rectangle_set_model" , :action=>'ajax_msg' , :id=>'1'},
  :remote => true,
  :class => 'link1',
  :update => "div_hello",
  :id => 'link1'
 %>
 </div>

 <div id="div_hello">
 </div>

---------------------
Controller - 'rectangle_set_model_controller.rb'

 def ajax_msg
  @msg1 = "Buenos Dias!"
  respond_to do
   |format|
   format.html
   format.js
   format.xml
  end
 end


---------------------
Javascript (corresponding to ajax_msg format.js in controller)
- 'ajax_msg.js'

$('#div_hello').html("<%= controller.msg1 %>");

---------------------

推荐答案

谢谢大家

塞缪尔-由于某些原因工作不正常

Samuel - not quite working for some reason

只要启用此行

format.js {render:json => {:text =>世界你好"}}

format.js {render :json => { :text => "Hello the world" }}

然后我在浏览器中出现错误(在操作链接上)

I then get error in browser (on action link)

浏览器弹出错误-------->预期为';'

browser popup error --------> Expected ';'

这篇关于关于简单的"Hello World" Rails-Ajax示例的想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:39