Permalink
Browse files

Can't do lottery now

  • Loading branch information...
1 parent a512b44 commit 912d3cf75887c2cf3fc91e5c689754b0a367ecee @miau715 committed Nov 11, 2011
@@ -56,9 +56,9 @@ def destroy
redirect_to event_path(@event)
end
- def lottery
+ #def lottery
#list_count = Cat.count
- @cats = Cat.lottery(params[:quantity])
- end
+ #@cats = Cat.lottery(params[:quantity])
+ #end
end
@@ -3,9 +3,10 @@ class EventsController < ApplicationController
before_filter :check_user, except: [:index]
def index
-
-
- @events = Event.all
+ if (current_user)
+ @user = current_user
+ @events = @user.events.all
+ end
end
def new
@@ -14,6 +15,7 @@ def new
def create
@event = Event.new(params[:event])
+ @event.user_id = current_user.id
@event.save
redirect_to events_path
@@ -42,6 +44,11 @@ def destroy
redirect_to events_path
end
+ def lottery
+ @event = Event.find(params[:id])
+ @cats = @event.cats.lottery(params[:quantity])
+ end
+
protected
def find_event
@event = Event.find(params[:id])
@@ -53,5 +60,4 @@ def check_user
redirect_to root_path
end
end
-
end
View
@@ -5,4 +5,6 @@ class Event < ActiveRecord::Base
has_many :cats
belongs_to :user
+ scope :lottery, lambda{|quantity| order('random()').limit(quantity)}
+
end
@@ -1,8 +1,13 @@
-<h1>活動列表</h1>
+<h1>首頁</h1>
+
+<% if user_signed_in? %>
+
+你的id是 <%= current_user.id %>
+
<ul>
<% @events.each do |list| %>
<li>
- <%= list.name %>
+ <%= list.name %> - <%= list.user_id %>
<%= link_to '顯示', event_path(list) %>
<%= link_to '編輯', edit_event_path(list) %>
<%= button_to '刪除', event_path(list), method: :delete %>
@@ -13,3 +18,16 @@
<%= link_to '建立新活動', new_event_path %>
</p>
+<% else %>
+
+<%= form_for("user", :url => user_session_path) do |f| %>
+ <%= f.text_field :email %> <br />
+ <%= f.password_field :password %> <br />
+ <%= f.check_box :remember_me %>
+ <%= f.label :remember_me %> <br />
+ <%= f.submit 'Sign in' %> <br />
+ <%= link_to "Forgot your password?", new_password_path('user') %>
+<% end %>
+
+
+<% end %>
@@ -0,0 +1,8 @@
+<h1>抽到的貓貓</h1>
+<p>抽到的貓貓是
+<% @cats.each do |cat| %>
+<br />「<%= cat.name %>
+<% end %>
+</p>
+
+<p><%= link_to '回名單', event_path(@event) %></p>
@@ -2,6 +2,13 @@
<p>活動名稱:<%= @event.name %></p>
<p>活動說明:<%= @event.description%></p>
+<%= form_tag url_for(action: 'lottery', controller: 'events'), :method => "get" do %>
+ <p>想抽
+ <%= text_field_tag 'quantity' %>
+ 隻貓</p>
+ <%= submit_tag '抽!' %>
+<% end %>
+
<ul>
<% @cats.each do |cat| %>
<li>
View
@@ -18,9 +18,10 @@
resources :events do
resources :cats do
- collection do
- get :lottery
- end
+ end
+
+ collection do
+ get :lottery
end
end
View
@@ -7,14 +7,14 @@
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
-Event.delete_all
-Cat.delete_all
+Event.destroy_all
+Cat.destroy_all
-Event.create(name: "我是新活動", description: "嘎哩噗哩啵!")
-Event.create(name: "活動二號", description: "我是活動二號")
+e1 = Event.create(name: "我是新活動", description: "嘎哩噗哩啵!", user_id: 1)
+e2 = Event.create(name: "活動二號", description: "我是活動二號", user_id: 2)
-Cat.create(name: "OREO", event_id: 1)
-Cat.create(name: "P醬", event_id: 1)
-Cat.create(name: "秒妙", event_id: 1)
-Cat.create(name: "甜不辣", event_id: 2)
-Cat.create(name: "豬血糕", event_id: 2)
+e1.cats.create(name: "OREO")
+e1.cats.create(name: "P醬")
+e1.cats.create(name: "秒妙")
+e2.cats.create(name: "甜不辣")
+e2.cats.create(name: "豬血糕")
View
Binary file not shown.

0 comments on commit 912d3cf

Please sign in to comment.