rails简单的ajax请求

时间:2012-09-20 11:24:57

标签: ruby-on-rails ajax

我遇到了ajax请求的问题:

在我的应用程序中我试图配置一个简单的排名系统,我设置它,但是我点击排名按钮 - 我重新加载页面和排名刷新。

帮助我在ajax中意识到:

我做到了:

posts/show.html.erb我有:

    <div id='vote_update'>
      <%= render 'vote/update' %>
    </div>
我在vote/_update.html.erb中的

<% if can? :update, Vote %>
    <%= form_for([@post, @post.vote], remote: true) do |f| %>
...
<%= f.submit "vote" %> 
   我在vote/update.js.erb中的

$('#vote_update').html("<%= j render('vote/update') %>");

并在vote_controller.rb中我有:

  def update
    post = Post.find(params[:post_id])
    vote = post.vote
...
    respond_to do |format|
        if vote.save
          format.html { 
            redirect_to post_path(post),
            :notice => "You vote counted!"
          }
          format.js
        end
    end
end

如果我删除romete:true - 一切顺利(页面重新加载,我看到(:通知)“你投票计数!”并且评级已更新,但如果我把遥控器:真正的回复,我什么都没看到(一些错误)在firebug控制台) - 但当我重新加载页面 - ratign更新,并且我看到norml结果 - 我认为我在重定向或我不知道时犯了一些错误

请帮助

2 个答案:

答案 0 :(得分:0)

变化

respond_to do |format|
    if vote.save
      format.html { 
        redirect_to post_path(post),
        :notice => "You vote counted!"
      }
      format.js
    end
end

要        如果vote.save     respond_to do | format |

      format.html { 
        redirect_to post_path(post),
        :notice => "You vote counted!"
      }
      format.js
    end
end

不确定

答案 1 :(得分:0)

你的ajax代码将以format.js块的形式出现。您可以在哪里更新显示排名的html。