【问题标题】:Call voting method, without refreshing page. Using Thumbs_Up gem and Rails调用投票方式,无需刷新页面。使用 Thumbs_Up gem 和 Rails
【发布时间】:2012-04-03 10:38:01
【问题描述】:

我已经为此苦苦挣扎了几个小时,但我根本无法超越我应该做的事情。我知道有几个人写过相同或类似的主题,但没有帮助。希望你能帮助我。

我正在尝试在我正在做的网站上创建一个投票系统,我正在为此使用 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;
});

希望你能帮助一个菜鸟:-)。

更新

视图的完整代码是:

<% @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 /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"} user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"} new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"} edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"} PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"} 删除 /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"} 根 / {:action=>"index", :controller=>"home"} new_sub_file /browse/:folder_id/new_file(.:format) {:action=>"new", :controller=>"images"} new_sub_folder /browse/:folder_id/new_folder(.:format) {:action=>"new", :controller=>"folders"} rename_folder /browse/:folder_id/rename(.:format) {:action=>"edit", :controller=>"folders"}

【问题讨论】:

  • 嗨,这可能会有所帮助:stackoverflow.com/questions/4907744/…欢呼!
  • 去过那里,做到了:-)。那些帖子帮助我让它工作,但是当你投票时它会刷新页面,这不是很实用。
  • :) 好朋友!刷新页面,即使它是一个 ajax 调用 hmm .. 所以问题在于 ajax - 这可能是 jsfiddle(我怀疑)或工作示例,对于 URL 尝试相对路径,cheerios!

标签: ruby-on-rails ruby ajax jquery


【解决方案1】:

如果你想发送一个 AJAX 请求,你必须在 url helper 中使用remote: true

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

【讨论】:

  • 然后我在链接的行上得到一个编译错误:/Users/holgersindbaek/Projekter/Seriously Mobile/Flow/app/views/images/index.html.erb:12:语法错误, 意外 ',', 期待 ')' ...', vote_up_image_path(image)), :remote => true, :method => :...
  • @HolgerEdwardWardlowSindbæk 确实,我已经更正了括号。
  • 刚刚做了一点更新...这与编译错误有关吗?
  • 太棒了...摆脱了编译错误。尽管如此,它仍然没有在数据库中投票。有什么建议? jquery错了吗?它甚至是正确的方法吗?
  • 如果我把return false;在 jquery 之外,我刷新后似乎正在注销我。为什么这样做?这和它有关系吗?
【解决方案2】:

在与 Nash 进行了长时间的交谈后,我最终创建了一个新项目。一切都很混乱。感谢纳什的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多