相关问题:Vote_fu and Ajax requests
我的Ajax请求似乎有问题。我想要做的是在事件发生时,点击投票,提交投票,然后更新页面,不刷新页面。
votes_controller.rb:
def create
@album = Album.find(params[:album_id])
respond_to do |format|
if current_user.vote(@album, params[:vote])
format.js { render :action => "create", :vote => @vote }
format.html { redirect_to([@album.user, @album]) }
#format.xml { render :xml => @album, :status => :created, :location => @album }
else
format.js { render :action => "error" }
format.html { render :action => "new" }
format.xml { render :xml => @vote.errors, :status => :unprocessable_entity }
end
end
endlink |用于查看相册显示:
<%= link_to_remote "Vote Up",
:url => user_album_votes_path(album.user, album,
:vote => :true, :format => :js),
:method => :post %>application.js
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
jQuery(document).ready(function() {
$("#votes_.album").bind('click');
})create.js
page.replace_html "#votes_{@album.id}",
:partial => "album_vote",
:locals => {:album => @album}这是我收到的以下错误消息:
missing ; before statement
[Break on this error] page.replace_html "#votes_#{@album.id}", ...bum_vote", :locals => {:album => @album}我不确定这里出了什么问题,我一直在遵循vote_fu文档中的许多示例,但仍然存在问题。
http://github.com/peteonrails/vote_fu/tree#readme
对create.js所做的一项修改
现在又出现了另一个错误:
否,错误已转移到votes_controller
NoMethodError (You have a nil object when you didn't expect it!
<br />
The error occurred while evaluating nil.vote):
app/controllers/votes_controller.rb:53:in `create'
app/controllers/votes_controller.rb:52:in `create'
<br />
Rendered rescues/_trace (128.4ms)
Rendered rescues/_request_and_response (0.4ms)
Rendering rescues/layout (internal_server_error)这些行在create操作上,看起来非常好!?
我怎么才能让它工作呢?
关注
丹
发布于 2010-03-05 18:19:36
尝试将create.js更改为
page.replace_html "#votes_#{@album.id}", :partial => "album_vote", :locals => {:album => @album}您可能遗漏了变量字符串插值的#。
发布于 2010-03-09 00:16:00
已解决!
问题是我没有添加一个before语句来刷新投票计数!所以我就这么做了,而且效果也很好,我把create.js改成了create.sj.erb,还对我的application.js文件做了一些小改动。在这之后,我添加了一个flash[:note] = you have voted!,然后添加了一个函数来删除一秒钟后的闪光通知和fadeOut!
对于任何感兴趣的人,这里有代码:
Application.js
jQuery(document).ready(function() {
$("#vote").bind('click', function(e) {
if (e.target.tagName == "DIV") {
$(this).find(".album_extened").toggle('blind');
}
})
})create.js.erb
$("#vote").before('<div id="notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#vote_count").html("<%= @album.votes_for - @album.votes_against %>");
$(document).ready(function() {
setTimeout(hideFlashMessages, 1000);
});
function hideFlashMessages() {
$("#vote, #notice").append('').fadeOut(1000);
}如果有人知道更好的原因,请转发!
[http://railscasts.com/episodes/136-jquery][1]的一个很好的观看
阅读链接文本
很快解决的事情感谢Jonathanlink text
1:http://railscasts.com/episodes/136-jquery/"Rails Cast ep 136“2:http://www.notgeeklycorrect.com/english/2009/05/18/beginners-guide-to-jquery-ruby-on-rails/”初学者协会到jQuery和Rails“,感谢萨姆的帮助!
发布于 2010-03-06 12:00:11
在我看来,create方法中的current_user没有得到它的值集。
如何设置current_user值?在其他插件中,他们通常将其定义为带有访问器的实例方法或实例变量。
因此,将create方法更改为以下内容可能会有所帮助:
def create
@album = Album.find(params[:album_id])
respond_to do |format|
if @current_user.vote(@album, params[:vote])
format.js { render :action => "create", :vote => @vote }
format.html { redirect_to([@album.user, @album]) }
#format.xml { render :xml => @album, :status => :created, :location => @album }
else
format.js { render :action => "error" }
format.html { render :action => "new" }
format.xml { render :xml => @vote.errors, :status => :unprocessable_entity }
end
end
end您可能错过了current_user.vote之前的@
https://stackoverflow.com/questions/2385321
复制相似问题