我一直在努力解决这个问题几个小时,我根本无法理解我应该如何做到这一点。我知道有几个人在同一主题或类似主题上写作,但它没有帮助。希望你能帮帮我。
我正在尝试在我正在进行的网站上建立一个投票系统,并且我正在使用它的宝石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 =&gt;“cancel”,:controller =&gt;“devise / registrations”} user_registration POST /users(.:format){:action =&gt;“create”,:controller =&gt;“devise / registrations”} new_user_registration GET /users/sign_up(.:format) {:action =&gt;“new”,:controller =&gt;“devise / registrations”} edit_user_registration GET /users/edit(.:format){:action =&gt;“edit”,:controller =&gt;“devise / registrations”} PUT /users(.:format) {:action =&gt;“update”,:controller =&gt;“devise / registrations”} DELETE /users(.:format){:action =&gt;“destroy”,:controller =&gt;“devise / registrations”} root / {:action =&gt;“index”,:controller =&gt;“home”} new_sub_file /browse/:folder_id/new_file(.:format) {:action =&gt;“new”,:controller =&gt;“images”} new_sub_folder /browse/:folder_id/new_folder(.:format) {:action =&gt;“new”,:controller =&gt;“folders”} rename_folder /browse/:folder_id/rename(.:format) {:action =&gt;“edit”,:controller =&gt;“folders”}
答案 0 :(得分:2)
如果您想发送AJAX请求,则必须在url helper中使用remote: true
:
<%= link_to('vote for this post!', vote_up_image_path(image), :remote => true, :method => :post) %>
答案 1 :(得分:0)
经过与纳什的长谈,我最终创建了一个新项目。一切都很乱。帮助纳什的谢谢。