【发布时间】:2012-03-10 05:05:45
【问题描述】:
我试图在我的 rails 应用程序的前端设置一个按钮,用于显示链接列表。我希望他们能够点击“添加到收藏夹”
现在我正在努力让它在没有 AJAX 的情况下工作,但最终我将在我得到后备工作后添加它(只是一个常规链接,对吗?......然后在 js 中我使用 return false; 然后使用 ajax)
现在,我没有看到任何错误,但是当我单击它时,它并没有添加收藏夹。任何帮助将不胜感激
路线:
root :to => 'home#index'
resources :resources, :except => [:index]
resources :profiles, :only => [:show]
resources :favorites, :only => [:create, :destroy]
match '/learn', :to => 'pages#learn'
match '/contact', :to => 'pages#contact'
match '/requests', :to => 'pages#requests'
最喜欢的控制器
class FavoritesController < ApplicationController
before_filter :authenticate_user!
def create
@favorite = Favorite.new(:resource_id => params[:id], :user_id => current_user.id)
if @favorite.valid?
@favorite.save
else
redirect_to root_url
end
end
def destroy
# also must find by user as well...
@favorite = Favorite.find_by_resource_id(params[:id])
@favorite.destroy
redirect_to root_url
end
end
这是我的视图代码,它实际上显示了“添加收藏夹”链接:
<%= link_to favorites_path(resource), :method => :post, :class => "btn btn-warning btn-mini" do %>
<i class="icon-star icon-white" rel="tooltip" title="add to favorites"></i> Add to favorites
<% end %>
当我将鼠标悬停在它上面时,网址显示为 site.com/favorites.3 --- 不确定这是否正确?
【问题讨论】:
-
点击链接时服务器端会发生什么?您是否从控制台中的请求中获得输出?
-
如果您还不知道,请查看github.com/seyhunak/twitter-bootstrap-rails。 :) 这是一个很好的引导程序
-
你的 rake 路由的结果是什么?悬停链接显然是错误的。
-
收藏夹 POST /favorites(.:format) favorites#create favorite DELETE /favorites/:id(.:format) favorites#destroy
标签: ruby-on-rails ruby ajax ruby-on-rails-3 activerecord