habitica/app/controllers/rewards_controller.rb

80 lines
1.9 KiB
Ruby
Raw Normal View History

2012-02-04 21:49:11 +00:00
class RewardsController < ApplicationController
# GET /rewards/new
# GET /rewards/new.json
def new
@reward = Reward.new
@reward.user_id = current_user.id
2012-02-04 21:49:11 +00:00
respond_to do |format|
format.html # new.html.erb
format.json { render json: @reward }
end
end
# GET /rewards/1/edit
def edit
@reward.user_id = current_user.id
2012-02-04 21:49:11 +00:00
end
# POST /rewards
# POST /rewards.json
def create
@reward = Reward.new(params[:reward])
@reward.user_id = current_user.id
2012-02-04 21:49:11 +00:00
respond_to do |format|
if @reward.save
format.html { redirect_to habits_url, notice: 'Reward was successfully created.' }
2012-02-04 21:49:11 +00:00
format.json { render json: @reward, status: :created, location: @reward }
else
format.html { render action: "new" }
format.json { render json: @reward.errors, status: :unprocessable_entity }
end
end
end
# PUT /rewards/1
# PUT /rewards/1.json
def update
@reward = curren_user.rewards.find(params[:id])
2012-02-04 21:49:11 +00:00
respond_to do |format|
if @reward.update_attributes(params[:reward])
format.html { redirect_to habits_url, notice: 'Reward was successfully updated.' }
2012-02-04 21:49:11 +00:00
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @reward.errors, status: :unprocessable_entity }
end
end
end
# DELETE /rewards/1
# DELETE /rewards/1.json
def destroy
@reward = current_user.reward.find(params[:id])
2012-02-04 21:49:11 +00:00
@reward.destroy
respond_to do |format|
format.html { redirect_to habits_url }
2012-02-04 21:49:11 +00:00
format.json { head :no_content }
end
end
def buy
@reward = current_user.rewards.find(params[:id])
current_user.money -= @reward.value
current_user.save
respond_to do |format|
# format.html { render action: "edit" }
# format.json { render json: @habit.errors, status: :unprocessable_entity }
format.js
end
end
2012-02-04 21:49:11 +00:00
end