Вызов метода голосования, без обновления страницы.Использование Thumbs_Up gem и Rails - PullRequest
0 голосов
/ 19 марта 2012

Я боролся с этим уже пару часов и просто не могу понять, как я должен это делать.Я знаю, что было несколько человек, пишущих на одну и ту же тему или похожую тему, но это не помогло.Надеюсь, вы мне поможете.

Я пытаюсь создать систему голосования на сайте, которым занимаюсь, и использую для этого самоцвет 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 из: -).

ОБНОВЛЕНИЕ

Полный код представления:

<% @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"} DELETE /users(.:format) {: action => "destroy",: controller => "devise / registrations "} root / {: 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 => "folder"} rename_folder /browse/:folder_id/rename(.:format) {: action=> "edit",: controller => "folder"}

Ответы [ 2 ]

2 голосов
/ 19 марта 2012

Если вы хотите отправить AJAX-запрос, вы должны использовать remote: true в url helper:

<%= link_to('vote for this post!', vote_up_image_path(image), :remote => true, :method => :post) %>
0 голосов
/ 19 марта 2012

После долгого разговора с Нэшем я закончил тем, что создал новый проект. Все было грязно. Спасибо за помощь Nash.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...