Last active
December 30, 2015 07:49
-
-
Save MartinMcDermid/7798297 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
class Lottery < ActiveRecord::Base | |
attr_accessible :product_id, :completed, :tickets_attributes | |
belongs_to :product | |
has_many :tickets | |
accepts_nested_attributes_for :tickets, :allow_destroy => 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
def index | |
@lotteries = Lottery.paginate(:page => params[:page], :per_page => 5, :order => 'created_at desc, id') | |
@completed_lotteries = Lottery.find(:all, :conditions => ["completed = ?", true]) | |
end | |
def show | |
@lottery = Lottery.find(params[:id]) | |
@tickets = Ticket.find(:all, :conditions => ["lottery_id = ?", params[:id]]) | |
end | |
def new | |
@lottery = Lottery.new | |
end | |
def create | |
@lottery = Lottery.new(params[:lottery]) | |
@product_tickets = @lottery.product.tickets | |
@product_tickets.times { @tickets = @lottery.tickets.build } | |
if @lottery.save | |
redirect_to @lottery, :notice => "Successfully created lottery." | |
else | |
render :action => 'new' | |
end | |
end | |
def edit | |
@lottery = Lottery.find(params[:id]) | |
end | |
def update | |
@lottery = Lottery.find(params[:id]) | |
if @lottery.update_attributes(params[:lottery]) | |
redirect_to @lottery, :notice => "Successfully updated lottery." | |
else | |
render :action => 'edit' | |
end | |
end | |
def destroy | |
@lottery = Lottery.find(params[:id]) | |
@lottery.destroy | |
redirect_to lotteries_url, :notice => "Successfully destroyed lottery." | |
end | |
def join | |
@lottery = Lottery.find(params[:id]) | |
@ticket = Ticket.find(params[:id]) | |
@ticket.update_attribute(:user_id, 500) | |
@ticket.save | |
puts @ticket | |
puts @ticket.id | |
puts @ticket.user_id | |
redirect_to @lottery | |
end | |
def leave | |
@lottery = Lottery.find params[:id] | |
current_user.update_attribute(:team_id, nil) | |
redirect_to @lottery | |
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
<% if ticket.user_id.nil? %> | |
<%= link_to 'join', join_lottery_path(lottery) %> | |
<% else %> | |
<li><a class="closed" href="#"><%= ticket.user_id %> closed</a></li> | |
<% 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
resources :categories | |
resources :products do | |
resources :tickets | |
end | |
resources :lotteries do | |
member do | |
get 'join' | |
get 'leave' | |
end | |
resources :tickets | |
end | |
authenticated :user do | |
root :to => 'home#index' | |
end | |
devise_for :users do | |
get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session | |
end | |
resources :users | |
root :to => "home#index" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment