¿Cómo hacer que este POST funcione en rieles?

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'

favorites Controller

class FavoritesController < ApplicationController

  before_filter :authenticate_user!

  def create
    @favorite = Favorite.new(:resource_id => params[:id], :user_id => current_user.id)
    if @favorite.valid?
      redirect_to root_url

  def destroy
    # also must find by user as well...
    @favorite = Favorite.find_by_resource_id(params[:id])
    redirect_to root_url


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

what happens on the server side when you click the link? do you get output from request in the console? -

deberías comprobar github.com/seyhunak/twitter-bootstrap-rieles if you don't already know it. :) It's a nice gem for bootstrap -

What's the result of your rake routes? The hover link is clearly wrong. -

favorites POST /favorites(.:format) favorites#create favorite DELETE /favorites/:id(.:format) favorites#destroy -

2 Respuestas

By default create actions are post in rails hence rather than changing default behaviour add new action for your purpose.

inside routes

resources :favorites, :only   => [:destroy] do
 get :add, :on => :collection 


<%= 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  %>

controlador create action will get renamed a add

respondido 10 mar '12, 09:03

I guess I'm still confused. If I'm not trying to rename an action do I still do this? I thought if its an ajax call to create a 'favorite' it would be FavoritesController#create - Chico alto

could you explain what this is doing? add_favorites_path(:id => resource) .. it worked and i have no idea why :/ I also dont know what this is get :add, :on => :collection . i really appreciate your help'! - Chico alto

we defined get route for url "/favourites/add"..for more info just go through guides.rubyonrails.org/routing.html - Sandip Ransing

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"

respondido 10 mar '12, 05:03

I'm telling it to post in the link_to (see last block of code). The route already is expecting POST because I used resources :favorites - Chico alto

Ok, i think the problem its favorites_path(resource), because it seams you are passing resource as a favorite, so it tries to get the favorite with that resource id. - Ismael

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.