Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Subscriptions

  • Loading branch information...
commit 413862cfdc32d0f933c6314e0959d29d73afb4e8 1 parent a231c4f
@bogdan authored
View
18 app/controllers/subscriptions_controller.rb
@@ -0,0 +1,18 @@
+class SubscriptionsController < ApplicationController
+
+ def index
+ @subscriptions = Subscription.all
+ @subscription = Subscription.new
+ end
+
+ def create
+ @subscription = Subscription.new(params[:subscription])
+
+ if @subscription.save
+ flash[:notice] = 'Subscription was successfully created.'
+ render :json => {:redirect => subscriptions_path}
+ else
+ render :json => {:errors => @subscription.errors}
+ end
+ end
+end
View
8 app/models/subscription.rb
@@ -0,0 +1,8 @@
+class Subscription < ActiveRecord::Base
+
+ validates_format_of :email, :with => /^[A-Z0-9_\.%\+\-']+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)$/i, :message => "Email is invalid"
+ validates_uniqueness_of :email, :message => "Email has already been taken"
+ validates_presence_of :first_name, :message => "First name is blank"
+ validates_presence_of :last_name, :message => "Last name is blank"
+
+end
View
5 app/views/layouts/application.html.erb
@@ -42,7 +42,7 @@
</div>
<% end %>
<p>
- Try to login or register with empty or invalid email to see the ajax validation.
+ Try to subscribe or register with empty or invalid email to see the ajax validation.
</p>
<div id="nav">
<h4>
@@ -50,9 +50,8 @@
<% if current_user %>
<%= link_to "Edit Profile", edit_user_path(current_user.id) %> |
<%= link_to "Logout", :logout %>
- <% else %>
- <%= link_to "Login", :login %> |
<% end %>
+ <%= link_to "Subscribe", subscriptions_path %>
</h4>
</div>
<%= yield %>
View
2  app/views/shared/_form_source.html.erb
@@ -0,0 +1,2 @@
+<h3>HTML:</h3>
+<pre class="sh_html"><code><%= form.to_str.gsub(/<div style=.*<\/div>/, "") %></code></pre>
View
3  app/views/shared/_source.html.erb
@@ -11,8 +11,7 @@
<%= ajaxsubmit_skin.capitalize %>
</h2>
</div>
- <h3>HTML:</h3>
- <pre class="sh_html"><code><%= form.to_str.gsub(/<div style=.*<\/div>/, "") %></code></pre>
+ <%= render :partial => 'shared/form_source', :locals => {:form => form} %>
<h3>JavaScript:</h3>
<pre class="sh_javascript"><code><%= File.read(Rails.root.join("public/javascripts/skins/#{ajaxsubmit_skin}.js")) %></code></pre>
<h3>CSS:</h3>
View
36 app/views/subscriptions/index.html.erb
@@ -0,0 +1,36 @@
+<div class="left spaced">
+ <h3>Subscribe to newsletter:</h3>
+ <h5>
+ This form show how different error styles could be combined in one form
+ </h5>
+ <%= form = form_for(@subscription, :html => {:class => "ajax-form"}) do |f| %>
+ <div class="field" validate="first_name">
+ <%= f.label :first_name %><br />
+ <%= f.text_field :first_name, :size => nil%>
+ </div>
+ <div class="field" validate="last_name">
+ <%= f.label :last_name %><br />
+ <%= f.text_field :last_name, :size => nil%>
+ <div class="validation-custom green">
+ <span class="validation-message"></span>
+ </div>
+ </div>
+ <div class="field" validate="email">
+ <div class="left">
+ <%= f.label :email, "Email (can be fake)" %><br />
+ <%= f.text_field :email, :size => nil %>
+ </div>
+ <div class="validation-custom red">
+ <span class="validation-message"></span>
+ </div>
+ </div>
+ <div class="clear"></div>
+ <div class="actions">
+ <%= f.submit :id => nil %>
+ </div>
+ <% end %>
+</div>
+
+<div class="left">
+ <%= render :partial => "shared/form_source", :locals => {:form => form} %>
+</div>
View
57 config/routes.rb
@@ -15,6 +15,7 @@
get :change
end
end
+ resources :subscriptions
resources :user_sessions
resources :password_resets
@@ -26,60 +27,4 @@
end
match "oauth/:provider" => "oauths#oauth", :as => :auth_at_provider
- # The priority is based upon order of creation:
- # first created -> highest priority.
-
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get 'short'
- # post 'toggle'
- # end
- #
- # collection do
- # get 'sold'
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get 'recent', :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- # root :to => "welcome#index"
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id(.:format)))'
end
View
15 db/migrate/20120620111857_create_subscriptions.rb
@@ -0,0 +1,15 @@
+class CreateSubscriptions < ActiveRecord::Migration
+ def self.up
+ create_table :subscriptions do |t|
+ t.string :email
+ t.string :first_name
+ t.string :last_name
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :subscriptions
+ end
+end
View
10 db/schema.rb
@@ -10,7 +10,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120314153132) do
+ActiveRecord::Schema.define(:version => 20120620111857) do
+
+ create_table "subscriptions", :force => true do |t|
+ t.string "email"
+ t.string "first_name"
+ t.string "last_name"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "user_providers", :force => true do |t|
t.integer "user_id", :null => false
View
24 public/stylesheets/scaffold.css
@@ -99,3 +99,27 @@ div.field, div.actions {
width: 239px;
height: 50px;
}
+
+
+.validation-active .validation-custom {
+ display: block;
+}
+.validation-custom {
+ display: none;
+ padding-top: 23px;
+ padding: 5px;
+ border-radius: 7px;
+ color: white;
+}
+
+.validation-custom.red {
+ margin: 5px;
+ float: left;
+ background-color: #F33;
+ border: 1px solid red;
+}
+.validation-custom.green {
+ width: 126px;
+ background-color: #3F3;
+ border: 1px solid green;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.