Last active
December 12, 2015 09:08
-
-
Save fidel/4748718 to your computer and use it in GitHub Desktop.
Autentykacja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
views/users/_form.html.erb --> | |
<div class="depot_form"> | |
<%= form_for @user do |f| %> | |
# ... | |
<fieldset> | |
<legend>Enter User Details</legend> | |
<div> | |
<%= f.label :name %>: | |
<%= f.text_field :name, size: 40 %> | |
</div> | |
<div> | |
<%= f.label :password, 'Password' %>: | |
<%= f.password_field :password, size: 40 %> | |
</div> | |
<div> | |
<%= f.label :password_confirmation, 'Confirm' %>: | |
<%= f.password_field :password_confirmation, size: 40 %> | |
</div> | |
<div> | |
<%= f.submit %> | |
</div> | |
</fieldset> | |
<% end %> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdminController < ApplicationController | |
def index | |
@total_orders = Order.count | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- app/views/admin/index.html.erb --> | |
<h1>Welcome</h1> | |
It's <%= Time.now %> | |
We have <%= pluralize(@total_orders, "order") %>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails generate controller Sessions new create destroy | |
rails generate controller Admin index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- app/views/sessions/new.html.erb --> | |
<div class="depot_form"> | |
<% if flash[:alert] %> | |
<p id="notice"><%= flash[:alert] %></p> | |
<% end %> | |
<%= form_tag do %> | |
<fieldset> | |
<legend>Please Log In</legend> | |
<div> | |
<%= label_tag :name, 'Name:' %> | |
<%= text_field_tag :name, params[:name] %> | |
</div> | |
<div> | |
<%= label_tag :password, 'Password:' %> | |
<%= password_field_tag :password, params[:password] %> | |
</div> | |
<div> | |
<%= submit_tag "Login" %> | |
</div> | |
</fieldset> | |
<% end %> | |
</div> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/controllers/sessions_controller.rb | |
def create | |
user = User.find_by_name(params[:name]) | |
if user and user.authenticate(params[:password]) | |
session[:user_id] = user.id | |
redirect_to admin_url | |
else | |
redirect_to login_url, alert: "Invalid user/password combination" | |
end | |
end | |
def destroy | |
session[:user_id] = nil | |
redirect_to store_url, notice: "Logged out" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/users_controller.rb | |
def index | |
@users = User.order(:name) | |
respond_to do |format| | |
format.html # index.html.erb | |
format.json { render json: @users } | |
end | |
end | |
def create | |
@user = User.new(params[:user]) | |
respond_to do |format| | |
if @user.save | |
format.html { redirect_to users_url, notice: "User #{@user.name} was successfully created." } | |
format.json { render json: @user, | |
status: :created, location: @user } | |
else | |
format.html { render action: "new" } | |
format.json { render json: @user.errors, | |
status: :unprocessable_entity } | |
end | |
end | |
end | |
def update | |
@user = User.find(params[:id]) | |
respond_to do |format| | |
if @user.update_attributes(params[:user]) | |
format.html { redirect_to users_url, notice: "User #{@user.name} was successfully updated." } | |
format.json { head :no_content } | |
else | |
format.html { render action: "edit" } | |
format.json { render json: @user.errors, | |
status: :unprocessable_entity } | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tional/users_controller_test.rb | |
class UsersControllerTest < ActionController::TestCase | |
setup do | |
@input_attributes = { | |
name: "sam", | |
password: "private", | |
password_confirmation: "private" | |
} | |
@user = users(:one) | |
end | |
# ... | |
test "should create user" do | |
assert_difference('User.count') do | |
post :create, user: @input_attributes | |
end | |
assert_redirected_to users_path | |
end | |
# ... | |
test "should update user" do | |
put :update, id: @user, user: @input_attributes | |
assert_redirected_to users_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment