Skip to content

Commit

Permalink
Merge pull request #2 from IliyanGanev/solo-iliyan
Browse files Browse the repository at this point in the history
Add some forms
  • Loading branch information
IliyanGanev authored May 6, 2017
2 parents 9016c69 + 3a8e25e commit 15ee022
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 60 deletions.
4 changes: 2 additions & 2 deletions app/controllers/session_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


post '/sessions' do
@user = User.where(username: params[:user][:username]).first
if @user && @user.authenticate?(params[:user][:password])
@user = User.where(username: params[:username]).first
if @user && @user.authenticate?(params[:password])
session[:id] = @user.id
redirect "/users/#{@user.id}"
else
Expand Down
61 changes: 30 additions & 31 deletions app/controllers/user_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# #index
# get '/users' do
# @users = User.all
# @users.map{|user| user.email.to_s}
# erb :'users/index'
# end
#index get all users info
get '/users' do
@users = User.all
@users.map{|user| user.email.to_s}
erb :'users/index'
end


#new
Expand Down Expand Up @@ -32,38 +32,37 @@
end


#edit
get '/users/:id/edit' do
@user = User.find(params[:id])
erb :'users/edit'
end

# #edit
# get '/users/:id/edit' do
# @user = User.find(params[:id])
# erb :'users/edit'
# end

# #update
# def update_user
# @user = User.find(params[:id])
# @user.update(params[:user])
# if @user.valid?
# redirect "/users/#{@user.id}"
# else
# @errors = @user.errors.full_messages
# erb :'users/edit'
# end
# end
#update
def update_user
@user = User.find(params[:id])
@user.update(params[:user])
if @user.valid?
redirect "/users/#{@user.id}"
else
@errors = @user.errors.full_messages
erb :'users/edit'
end
end

# patch '/users/:id' do
# update_user
# end

# put '/users/:id' do
# update_user
# end
put '/users/:id' do
update_user
end


# #delete
# delete '/users/:id' do
# User.find(params[:id]).destroy!
# redirect '/users'
# end
#delete
delete '/users/:id' do
User.find(params[:id]).destroy!
redirect '/users'
end


16 changes: 9 additions & 7 deletions app/views/sessions/new.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<h1> sessions/new page </h1>
<form method = "post" action = "/sessions">
<label>Username:</label>
<input type = "text" name = "user[username]">
<h1>Login</h1>
<br>
<label>Password:</label>
<input type = "password" name = "user[password]">
<br>
<input type = "submit", value = "Login">
<form action = "/sessions" method = "post">
<label>Username:</label><br>
<input type = "text" name = "username">
<br>
<label>Password:</label><br>
<input type = "password" name = "password">
<br>
<input type = "submit", value = "Login">
</form>

<%= erb :'_errors' %>
18 changes: 18 additions & 0 deletions app/views/users/edit.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>Edit user details</h1>


<form method="post" action="/users/<%= @user.id %>">
<input type="hidden" name="_method" value="put" />

<label for="username">username:</label><br>
<input id="user_name" name="user[username]" type="text" value="<%= @user.username %>"><br>

<label for="user_email">Email</label><br>
<input id="user_email" name="user[email]" type="text" value="<%=@user.email %>"/><br>

<label for="user_password">Password</label><br>
<input id="user_password" name="user[password]" type="password"/>

<input type="submit" value="Edit User" class="button">
</form>

18 changes: 18 additions & 0 deletions app/views/users/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>All Users</h1>
<div class="users">
<ul>
<% @users.each do |user| %>
<li><%= "#{user.username} - #{user.email}"%> - <a href="/users/<%=user.id %>">Show details</a>
<a href="/users/<%=user.id %>/edit">Edit</a>

<form method="post" action="/users/<%=user.id %>">
<input name="_method" type="hidden" value="delete" />
<input name="commit" type="submit" value="Delete"/>
</form>
</li>
<% end %>
</ul>
</div>



20 changes: 5 additions & 15 deletions app/views/users/new.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
<h1>NEW USER PAGE</h1>
</br>
</br>
<h1>New User Page</h1>

<form method = "post" action = "/users">
<label>username:</label>
<input type = "text" name = "user[username]">
</br>
<label>email:</label>
<input type = "email" name = "user[email]">
<br>
<label>Password:</label>
<input type = "password" name = "user[password]">
<br>
<input type = "submit", value = "Register User">
<input type = "text" name = "user[username]" value="<%=@user.username %>" placeholder="enter username"><br>
<input type = "email" name = "user[email]" value="<%=@user.email %>" placeholder="enter email"><br>
<input type = "password" name = "user[password]" placeholder="enter password"><br>
<input type = "submit", value = "Register User">
</form>



<%= erb :'_errors' %>
12 changes: 7 additions & 5 deletions app/views/users/show.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

<h1>Page showing User info with option to logout or do other things</h1>
<h1>users show</h1>

<h1><%= @user.username%> </h1>

<h1><%= @user.email%> </h1>
<form method = "post" action = "/sessions/:id">
<input type = "hidden" name = "_method" value = "delete">
<input type = "submit" value = "logout">
<input type = "hidden" name = "_method" value = "delete">
<input type = "submit" value = "logout">
</form>

<a href='/users'>show all users</a>



<a href='/restaurants/new'>DO A THING</a>

0 comments on commit 15ee022

Please sign in to comment.