Created
December 24, 2010 20:55
-
-
Save joshrendek/754505 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for([@ticketable, @ticket]) do |f| %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :user do | |
resources :tickets | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ticket < ActiveRecord::Base | |
belongs_to :ticketable, :polymorphic => true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TicketsController < ApplicationController | |
before_filter :get_object, :only => [:create, :new] | |
def create | |
@ticket = Ticket.new(params[:ticket]) | |
@ticket.ticketable = @ticketable | |
respond_to do |format| | |
if @ticket.save | |
format.html { redirect_to(@ticket, :notice => 'Ticket was successfully created.') } | |
format.xml { render :xml => @ticket, :status => :created, :location => @ticket } | |
else | |
format.html { render :action => "new" } | |
format.xml { render :xml => @ticket.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
#...other controller code | |
private | |
def get_object | |
type = '' | |
if params[:user_id]; type = 'user' | |
elsif params[:g_server_id]; type = 'g_server' | |
end | |
value = type.classify.constantize.find_by_identifier(params[:"#{type}_id"]) | |
instance_variable_set("@ticketable" , value) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# part of the user model | |
class User < ActiveRecord::Base | |
has_many :tickets, :as => :ticketable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment