Rails 渲染在控制台中工作但不加载页面
我的目标是通过 AJAX 调用呈现 html。在我的 AJAX 调用中将 dataType 更改为“html”似乎有效,因为在我的控制台上它很高兴地声称渲染成功:
Rendered admin/articles/show.html.erb within layouts/application (108.1ms)
Completed 200 OK in 185ms (Views: 112.6ms | ActiveRecord: 2.8ms)
但页面没有更改。
下面是一些代码:
View:
<span id='show' class="ui-icon ui-icon-extlink" article_id=<%=article.ID %> onmouseover="this.style.cursor='pointer'"></span>
jquery:
$('#show').live("click",function(evt){
evt.preventDefault();
var article_id=$(this).attr("article_id");
$.ajax({
success : null,
type : 'GET',
url : '/admin/articles/show',
dataType : 'html',
data: {
id: article_id
},
});
});
和我的控制器:
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @article }
end
end
routes.rb
match 'articles/show' => 'articles#show', :via => :get
我知道想要从 AJAX 渲染 html 是一个奇怪的请求,但这是我认为在 span 标记中包含可点击链接的最简单方法。
使用 Rails 3.0.1
、Ruby 1.9.2
和 JQuery
My goal is to render html from an AJAX call. Changing the dataType to 'html' in my AJAX call seemed to work in that on my console it happily claims that the render was successful:
Rendered admin/articles/show.html.erb within layouts/application (108.1ms)
Completed 200 OK in 185ms (Views: 112.6ms | ActiveRecord: 2.8ms)
but the page does not change.
Here's some code:
View:
<span id='show' class="ui-icon ui-icon-extlink" article_id=<%=article.ID %> onmouseover="this.style.cursor='pointer'"></span>
jquery:
$('#show').live("click",function(evt){
evt.preventDefault();
var article_id=$(this).attr("article_id");
$.ajax({
success : null,
type : 'GET',
url : '/admin/articles/show',
dataType : 'html',
data: {
id: article_id
},
});
});
and my controller:
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @article }
end
end
routes.rb
match 'articles/show' => 'articles#show', :via => :get
I know it's an odd request to want to render html from AJAX but it's the easiest way that I can think to have a clickable link contained in a span tag.
Using Rails 3.0.1
, Ruby 1.9.2
and JQuery
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常ajax请求ha js类型,如果你可以将类型更改为js,这些更改将在
控制器
端起
作用,然后在app/views/articles/show.js.erb中编写javascript代码来更新页面内容
,如下方法将适用于html和JS
usually ajax request ha js type,if you can change type to js these changes will works
controller
end
then in app/views/articles/show.js.erb write javascript code to update page content like
and following approach will work for both html and JS