Hi, I have a basic Rails question:
I''m creating a little widget that allows people to call recipes
"favorites".  But when they add a recipe, I don''t want them
to see the
"Add as a favorite" button, I want them to see a "Remove from
your
favorites" link. I''m not sure what I need to do in the controller
to
make this happen. Here''s what I have right now:
recipes_controller.rb:
  def favorite
    Recipe.find(params[:id]).favorites.create(params[:favorite])
    flash[:notice] = "You have added this recipe to your favorites."
    redirect_to :action => "show", :id => params[:id]
  end
show.rhtml:
<%= form_tag :controller => "recipes", :action =>
"favorite", :id =>
@recipe %>
  <%= hidden_field "favorite", "user_id", :value =>
current_user.id %>
  <%= submit_tag "Add as favorite", :class =>
"favorite" %>
</form>
and I want to replace it with:
<%= link_to ''Un-favorite this recipe'', :controller =>
"recipes", :action
=> "unfavorite", :recipe_id => @recipe, :user_id =>
current_user.id %>
-- 
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> I''m creating a little widget that allows people to call recipes > "favorites". But when they add a recipe, I don''t want them to see the > "Add as a favorite" button, I want them to see a "Remove from your > favorites" link. I''m not sure what I need to do in the controller to > make this happen. Here''s what I have right now: > > recipes_controller.rb: > > def favorite > Recipe.find(params[:id]).favorites.create(params[:favorite]) > flash[:notice] = "You have added this recipe to your favorites." > redirect_to :action => "show", :id => params[:id] > end > > show.rhtml: > > <%= form_tag :controller => "recipes", :action => "favorite", :id => > @recipe %> > <%= hidden_field "favorite", "user_id", :value => current_user.id %> > <%= submit_tag "Add as favorite", :class => "favorite" %> > </form> > > and I want to replace it with: > > <%= link_to ''Un-favorite this recipe'', :controller => "recipes", :action > => "unfavorite", :recipe_id => @recipe, :user_id => current_user.id %>In show.rhtml... <% if params[:action] == ''favorite'' %> ...put your un-favorite stuff here... <% else %> ...put your add as favorite stuff here... <% end %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom wrote:>> flash[:notice] = "You have added this recipe to your favorites." >> >> and I want to replace it with: >> >> <%= link_to ''Un-favorite this recipe'', :controller => "recipes", :action >> => "unfavorite", :recipe_id => @recipe, :user_id => current_user.id %> > > In show.rhtml... > > <% if params[:action] == ''favorite'' %> > ...put your un-favorite stuff here... > <% else %> > ...put your add as favorite stuff here... > <% end %>Ahh, that makes a lot of sense, though it doesn''t seem to work for me right now. Do I have to do anything in the controller at all, or should it work with just the params[:action] expression in the if statement? If it''s supposed to work with no alterations to the controller, I might have mixed something else up... :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave A. wrote:> Philip Hallstrom wrote: > >> flash[:notice] = "You have added this recipe to your favorites." > >> > >> and I want to replace it with: > >> > >> <%= link_to ''Un-favorite this recipe'', :controller => "recipes", :action > >> => "unfavorite", :recipe_id => @recipe, :user_id => current_user.id %> > > > > In show.rhtml... > > > > <% if params[:action] == ''favorite'' %> > > ...put your un-favorite stuff here... > > <% else %> > > ...put your add as favorite stuff here... > > <% end %> > > Ahh, that makes a lot of sense, though it doesn''t seem to work for me > right now. Do I have to do anything in the controller at all, or should > it work with just the params[:action] expression in the if statement? > If it''s supposed to work with no alterations to the controller, I might > have mixed something else up... :) > > -- > Posted via http://www.ruby-forum.com/.This doesn''t work because you''re redirecting to the "show" action. I''m guessing you''re saving the favorite relationship in the model, so why not check for that? if @user.favorites.include? @recipe --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---