Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added ratings with auth

  • Loading branch information...
commit 221090fc363fc20f51ae96ccf5e76d24ca03d2bf 1 parent fbc1029
@alterisian authored
View
2  app/controllers/application_controller.rb
@@ -1,3 +1,5 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ before_filter :authenticate_user!
end
View
30 app/controllers/ratings_controller.rb
@@ -1,9 +1,9 @@
class RatingsController < ApplicationController
# GET /ratings
# GET /ratings.xml
- def index
- @ratings = Rating.all
-
+ def index
+ @ratings = Rating.where('user_id=?',current_user.id).order("created_at DESC")
+
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @ratings }
@@ -34,17 +34,27 @@ def new
# GET /ratings/1/edit
def edit
- @rating = Rating.find(params[:id])
+ @rating = Rating.where(:user_id=>current_user.id).where(:id=>params[:id]).first
+ if @rating.nil?
+ #notify[:notice] => 'Cheak!'
+ redirect_to :ratings
+ end
end
# POST /ratings
# POST /ratings.xml
def create
@rating = Rating.new(params[:rating])
-
+ @rating.user = current_user
+
respond_to do |format|
if @rating.save
- format.html { redirect_to(@rating, :notice => 'Rating was successfully created.') }
+ msg = @rating.value.to_s
+ msg=msg+", for " +@rating.description unless (@rating.description.nil? or @rating.description.empty?)
+
+ flash[:notice] = 'Rating '+msg+' was successfully created.'
+
+ format.html { redirect_to(ratings_url) }
format.xml { render :xml => @rating, :status => :created, :location => @rating }
else
format.html { render :action => "new" }
@@ -56,11 +66,13 @@ def create
# PUT /ratings/1
# PUT /ratings/1.xml
def update
- @rating = Rating.find(params[:id])
+ @rating = Rating.where(:user_id=>current_user.id).where(:id=>params[:id]) #todo-protect from id?
respond_to do |format|
if @rating.update_attributes(params[:rating])
- format.html { redirect_to(@rating, :notice => 'Rating was successfully updated.') }
+ flash[:notice] = 'Rating was successfully updated.'
+
+ format.html { redirect_to(@rating) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
@@ -73,7 +85,7 @@ def update
# DELETE /ratings/1.xml
def destroy
@rating = Rating.find(params[:id])
- @rating.destroy
+ #@rating.destroy
respond_to do |format|
format.html { redirect_to(ratings_url) }
View
5 app/models/rating.rb
@@ -1,2 +1,7 @@
+#ref: http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates_numericality_of
class Rating < ActiveRecord::Base
+ has_one :user
+
+ validates_presence_of :value
+ validates_numericality_of :value, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 11, :only_integer => true
end
View
2  app/models/user.rb
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ has_many :ratings
+
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
View
8 app/views/layouts/application.html.erb
@@ -8,15 +8,21 @@
</head>
<body>
+<h1> Rate It </h1>
+
+<p style="color: green"><%= flash[:notice] %></p>
+
<div id="user_nav">
<% if user_signed_in? %>
- Signed in as <%= current_user.email %>. Not you?
+ Signed in as <%= current_user.email %>(<%=current_user.id %>) Not you?
<%= link_to "Sign out", destroy_user_session_path %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
<% end %>
</div>
+
+
<%= yield %>
</body>
View
18 app/views/ratings/edit.html.erb
@@ -1,6 +1,20 @@
<h1>Editing rating</h1>
-<%= render 'form' %>
+<% form_for(@rating) do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :value %><br />
+ <%= f.text_field :value %>
+ </p>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_field :description %>
+ </p>
+ <p>
+ <%= f.submit 'Update' %>
+ </p>
+<% end %>
<%= link_to 'Show', @rating %> |
-<%= link_to 'Back', ratings_path %>
+<%= link_to 'Back', ratings_path %>
View
18 app/views/ratings/index.html.erb
@@ -1,25 +1,25 @@
-<h1>Listing ratings</h1>
+<h1>Listing Ratings</h1>
+
+<%= link_to 'New Rating', new_rating_path %>
<table>
<tr>
+ <th>Time</th>
<th>Value</th>
<th>Description</th>
- <th></th>
- <th></th>
- <th></th>
</tr>
<% @ratings.each do |rating| %>
<tr>
- <td><%= rating.value %></td>
- <td><%= rating.description %></td>
- <td><%= link_to 'Show', rating %></td>
+ <td><%=h (rating.created_at+1.hour).to_s(:short) %></td>
+ <td><%=h rating.value %></td>
+ <td><%=h rating.description %></td>
<td><%= link_to 'Edit', edit_rating_path(rating) %></td>
- <td><%= link_to 'Destroy', rating, :confirm => 'Are you sure?', :method => :delete %></td>
+ <!-- <td><%= link_to 'Destroy', rating, :confirm => 'Are you sure?', :method => :delete %></td> -->
</tr>
<% end %>
</table>
<br />
-<%= link_to 'New Rating', new_rating_path %>
+<%= link_to 'New Rating', new_rating_path %>
View
22 app/views/ratings/new.html.erb
@@ -1,5 +1,21 @@
-<h1>New rating</h1>
+<h1>New Rating</h1>
-<%= render 'form' %>
+<% form_for(@rating) do |f| %>
+ <%= f.error_messages %>
-<%= link_to 'Back', ratings_path %>
+ <p>
+ <%= f.label :value %>(1-10)<br />
+ <%= f.text_field :value, :size=>3 %>
+ </p>
+
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_field :description %>
+ </p>
+ <p>
+ <%= f.submit 'Create' %>
+ </p>
+<% end %>
+
+<%= link_to 'List Ratings', ratings_path %>
+(i.e. rating of your enjoyment of a band, and then description of who & where you are watching them, and your initial.)
View
8 app/views/ratings/show.html.erb
@@ -1,15 +1,13 @@
-<p id="notice"><%= notice %></p>
-
<p>
<b>Value:</b>
- <%= @rating.value %>
+ <%=h @rating.value %>
</p>
<p>
<b>Description:</b>
- <%= @rating.description %>
+ <%=h @rating.description %>
</p>
<%= link_to 'Edit', edit_rating_path(@rating) %> |
-<%= link_to 'Back', ratings_path %>
+<%= link_to 'Back', ratings_path %>
View
2  config/routes.rb
@@ -1,4 +1,6 @@
Templateapp::Application.routes.draw do
+ resources :commentries
+
devise_for :users
resources :ratings
View
14 db/migrate/20110623161206_create_ratings.rb
@@ -1,14 +0,0 @@
-class CreateRatings < ActiveRecord::Migration
- def self.up
- create_table :ratings do |t|
- t.integer :value
- t.string :description
-
- t.timestamps
- end
- end
-
- def self.down
- drop_table :ratings
- end
-end
View
1  db/schema.rb
@@ -13,6 +13,7 @@
ActiveRecord::Schema.define(:version => 20110623163817) do
create_table "ratings", :force => true do |t|
+ t.integer "user_id"
t.integer "value"
t.string "description"
t.datetime "created_at"
View
2  tmp/pids/server.pid
@@ -1 +1 @@
-1840
+4804
Please sign in to comment.
Something went wrong with that request. Please try again.