Replacing code in the view

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

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... :slight_smile:

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