Skip to content

Instantly share code, notes, and snippets.

@avk
Created February 24, 2009 07:38
Show Gist options
  • Save avk/69465 to your computer and use it in GitHub Desktop.
Save avk/69465 to your computer and use it in GitHub Desktop.
# Here's a sample from one of my controllers:
def create
@feed_search = FeedSearch.new(params[:feed_search])
@feed_search.feeds = params[:feeds].delete_if{|url| url==""}.map{|url| Feed.find_or_initialize_by_url(url)}
@user = (logged_in?) ? current_user : User.find_by_email_or_register(params[:feed_search][:email])
# search needs activation if user isn't logged in
@feed_search.state = (logged_in?) ? 'active' : 'pending'
if @feed_search.save
@result = logged_in? ? 'created' : @user.active? ? 'login' : 'signup'
# associate the user and the search
@user.feed_searches << @feed_search
else
@result = 'error'
end
end
# NOTE: how about a respond_to module inside the controller
# whose methods are the names of actions
# (e.g. def create in the module v. def respond_to_create in the controller)
def respond_to_create
respond_to do |format|
if @result != 'error'
format.html { redirect_to feed_search_path(@feed_search) }
format.xml { render :xml => @feed_search, :status => :created, :location => @feed_search }
else
format.html { render :action => "new" }
format.xml { render :xml => @feed_search.errors, :status => :unprocessable_entity }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment