I'm trying to have a button on the front-end of my rails app which shows a list of links. I want them to be able to click "Add to Favorite"
Right now I'm struggling on just making it work without AJAX, but eventually I'm going to add that in once I get the fallback working (just a regular link, right?.. then in the js i use return false; and then use ajax)
Right now, i dont see anything that is wrong, but its not adding a favorite when i click it. Any help would be appreciated
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
heres my Views code that actually shows the 'add favorite' link:
<%= 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 %>
when i hover over it the url shows up as site.com/favorites.3 --- not sure if this is correct?
preguntado el 10 de marzo de 12 a las 05:03
By default create actions are post in rails hence rather than changing default behaviour add new action for your purpose.
resources :favorites, :only => [:destroy] do get :add, :on => :collection end
<%= link_to add_favorites_path(:id => resource), :class => "btn btn-warning btn-mini" do %> <i class="icon-star icon-white" rel="tooltip" title="add to favorites"></i> Add to favorites <% end %>
create action will get
I think you can do that using a form to POST data instead of a link that makes GET, or adding this route should make it as well
match "/favorites/add/:id" => "favorites#create"