2012-03-18 44 views
0

我一直在为此挣扎几个小时,而我根本无法过去我应该如何去做。我知道有几个人写同一主题或类似的东西,但没有帮助。希望您能够帮助我。调用投票方法,无需刷新页面。使用Thumbs_Up宝石和Rails

我正在尝试在我正在做的网站上制作投票系统,并且我正在为它配置gem Thumbs_Up。它工作正常,除了每次投票时重新加载站点。

我images_controller.rb是这样的:

def vote_up 
begin 
    current_user.vote_for(@image = Image.find(params[:id])) 
    render :nothing => true, :status => 200 
rescue ActiveRecord::RecordInvalid 
    render :nothing => true, :status => 404 
end 
end 

我index.html.erb是这样的:

<%= link_to('vote for this post!', vote_up_image_path(image)) %> 

我正与一些JavaScript很努力,但它不工作:

$(".shotBG a").click(function(){ 
    $.ajax({ 
     type: 'PUT', 
     url: "http://localhost:3000/images/37/vote_up", 
     success: function(){ 
      alert("hello"); 
     } 
    }); 
    return false; 
}); 

希望你能帮助noob out :-)。

UPDATE

的查看完整的代码是:

<% @images.each do |image| %> 
    <li class="shotBG bottomShots"> 
     <img class="shot" src="<%= image.uploaded_file.url %>" alt="Competing for dribbble invites." /> 
     <%= link_to('vote for this post!', vote_up_image_path(image)), :remote => true, :method => :post %> 
    </li> 
<% end %> 

这可能有一些做的编译错误?

更新2

忘记展示我的路由文件:

resources :images do 
    member do 
     post :vote_up 
    end 
end 

更新3

耙路线:

browse  /browse/:folder_id(.:format)   {:action=>"browse", :controller=>"home"} 
     vote_up_image POST /images/:id/vote_up(.:format)   {:action=>"vote_up", :controller=>"images"} 
       images GET /images(.:format)      {:action=>"index", :controller=>"images"} 
        POST /images(.:format)      {:action=>"create", :controller=>"images"} 
      new_image GET /images/new(.:format)     {:action=>"new", :controller=>"images"} 
      edit_image GET /images/:id/edit(.:format)    {:action=>"edit", :controller=>"images"} 
       image GET /images/:id(.:format)     {:action=>"show", :controller=>"images"} 
        PUT /images/:id(.:format)     {:action=>"update", :controller=>"images"} 
        DELETE /images/:id(.:format)     {:action=>"destroy", :controller=>"images"} 
      folders GET /folders(.:format)      {:action=>"index", :controller=>"folders"} 
        POST /folders(.:format)      {:action=>"create", :controller=>"folders"} 
      new_folder GET /folders/new(.:format)     {:action=>"new", :controller=>"folders"} 
     edit_folder GET /folders/:id/edit(.:format)    {:action=>"edit", :controller=>"folders"} 
       folder GET /folders/:id(.:format)     {:action=>"show", :controller=>"folders"} 
        PUT /folders/:id(.:format)     {:action=>"update", :controller=>"folders"} 
        DELETE /folders/:id(.:format)     {:action=>"destroy", :controller=>"folders"} 
        GET /images(.:format)      {:action=>"index", :controller=>"images"} 
        POST /images(.:format)      {:action=>"create", :controller=>"images"} 
        GET /images/new(.:format)     {:action=>"new", :controller=>"images"} 
        GET /images/:id/edit(.:format)    {:action=>"edit", :controller=>"images"} 
        GET /images/:id(.:format)     {:action=>"show", :controller=>"images"} 
        PUT /images/:id(.:format)     {:action=>"update", :controller=>"images"} 
        DELETE /images/:id(.:format)     {:action=>"destroy", :controller=>"images"} 
    new_user_session GET /users/sign_in(.:format)    {:action=>"new", :controller=>"devise/sessions"} 
     user_session POST /users/sign_in(.:format)    {:action=>"create", :controller=>"devise/sessions"} 
destroy_user_session DELETE /users/sign_out(.:format)    {:action=>"destroy", :controller=>"devise/sessions"} 
     user_password POST /users/password(.:format)    {:action=>"create", :controller=>"devise/passwords"} 
    new_user_password GET /users/password/new(.:format)   {:action=>"new", :controller=>"devise/passwords"} 
    edit_user_password GET /users/password/edit(.:format)   {:action=>"edit", :controller=>"devise/passwords"} 
        PUT /users/password(.:format)    {:action=>"update", :controller=>"devise/passwords"} 

cancel_user_registration GET /用户/取消(:格式){:action =>“cancel”,:controller =>“设计/注册”} user_registration POST /users(.:format){:action =>“create”,:controller =>“devise/registrations”} new_user_registration GET /users/sign_up(.:format){:action =>“new”,:控制器=>“设计/注册”} edit_user_registration GET /users/edit(.:format){:action =>“edit”,:controller =>“devise/registrations”} PUT /users(.:format){ :动作=>“更新”,::控制器=>“设计/注册”} 根/ {:动作=>“破坏”,:控制器=>“设计/注册”} root/{ :action =>“index”,:controller =>“home”} new_sub_folder />:浏览/:folder_id/new_folder(。:format){:action =>“new”,:controller =>“folders”} rename_folder /browse/:folder_id/rename(.:format){:动作=> “编辑”,:控制器=> “文件夹”}

+0

嗨,这可能有帮助:http:// stackoverflow。com/questions/4907744 /关于如何使用拇指向上投票的问题澄清说明 - 宝石与铁轨-3欢呼! – 2012-03-18 20:26:36

+0

在那里,做到了:-)。这些帖子帮助我实现了它的效果,但是当您投票时它会刷新页面,这不太实际。 – 2012-03-18 20:39:50

+0

:) Saweet队友!刷新页面,即使它的ajax调用嗯..所以问题是与在ajax - 任何可能这可能是jsfiddle(我怀疑)或工作示例,对于URL尝试相对路径,cheerios! – 2012-03-18 20:52:19

回答

0

经过与纳什的长谈,我最终创建了一个新项目。一切都很混乱。谢谢你的帮助纳什。

2

如果您想发送一个AJAX请求,您必须使用remote: true在网址帮手:

<%= link_to('vote for this post!', vote_up_image_path(image), :remote => true, :method => :post) %> 
+0

然后,我只是在链接的一行中得到一个编译错误:/ Users/holgersindbaek/Projekter/Seriously Mobile/Flow/app/views/images/index.html.erb:12:语法错误,意外的',',期待')' ...',vote_up_image_path(image)),:remote => true,:method =>:... – 2012-03-18 21:26:35

+0

@HolgerEdwardWardlowSindbæk确实,我更正了括号。 – 2012-03-18 21:41:34

+0

只是做了一个小小的更新...可以与编译错误有关吗? – 2012-03-18 21:41:49