Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #What im trying to do is to create a user signup, i have a user sessions #created aswell (for the user session.. using a different controller)
- #(as you may also be able to tell, is that I am a rails noob)
- -------------------------------------------------------------
- # my create view (to register new user):
- <h3>Register</h3>
- <%= token_tag %>
- <%form_for :user do |form| %>
- <fieldset>
- <legend>Please enter your details</legend>
- <%= error_messages_for "user" %>
- <div class="FormRow">
- <label for="login:">Full name:</label>
- <%= form.text_field :name, :size =>20, :maxlength =>30 %>
- <br />
- </div>
- <div class="FormRow">
- <label for="login:">Screen name:</label>
- <%= form.text_field :login, :size =>20, :maxlength =>20 %>
- <br />
- </div>
- <label for="email:">Email Address:</label>
- <%= form.text_field :email, :size =>20, :maxlength => 50 %>
- <br />
- <div class="FormRow">
- <label for="password:">Password:</label>
- <%= form.password_field :password, :size =>20, :maxlength => 20 %>
- <br />
- </div>
- <div class="FormRow">
- <%= submit_tag "Register!", :class => "submit" %>
- </div>
- </fieldset>
- <% end %>
- --------------------------------------------------------------
- #USER CONTROLLER ... linked to create.html.erb view above ^^
- class UserController < ApplicationController
- def index
- end
- def destroy
- end
- def create #this is used as register, the destroy is used when the user wishes to delete their account
- @title = "Register"
- if request.post? and params[:user]
- @user = User.new(params[:user])
- if @user.save
- render :text => "User Created!"
- end
- end
- end
- end
- -----------------------------------------------------------------
- # USER model
- class User < ActiveRecord::Base
- has_many :stories
- has_many :votes
- end
- validates_uniqueness_of :login
- validates_length_of :login, :within => 5..20
- validates_length_of :password, :within => 5..20
- validates_length_of :email, :maximum => 50
- validates_length_of :name, :within => 2..30
- validates_presence_of :email, :login, :password
- validates_format_of :login,
- :with => /^[A-Z0-9_]*$/i,
- :message => "Can contain only letters," + "numbers and underscores [A-Z, 0-9, _]"
- validates_format_of :email,
- :with => /^[A-Z0-9._%-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i,
- :message => "Email address must be valid."
- def validate
- errors.add(:email, "Please enter a valid email address") unless email.include? ("@")
- if login.include?(" ")
- errors.add(:login, "Screen name cannot include spaces")
- end
- end
- -------------------------------------------------------------------
- #sessions controller, for the user session:
- class SessionsController < ApplicationController
- def new
- end
- def create
- @current_user = User.find_by_login_and_password(
- params[:login], params[:password])
- if @current_user
- session[:user_id] = @current_user.id
- if session[:return_to]
- redirect_to session[:return_to]
- session[:return_to] = nil
- else
- redirect_to stories_path
- end
- else
- render :action => 'new'
- end
- end
- def destroy
- session[:user_id] = @current_user =nil
- end
- end
- ------------------------------------------------------------------------
- #create sessions view
- <% form_tag session_path do %>
- <%= token_tag %>
- <p> Login in please. </p>
- <p>
- <label> Username:</lable>
- <%= text_field_tag 'login' %>
- </p>
- <p>
- <label> Password:</label>
- <%= password_field_tag 'password' %>
- </p>
- <p><%= submit_tag 'login' %></p>
- <% end %>
- ------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment