(employees_controller.rb)
.
.
.
# When the user clicks "Make User employee" this method fires up
# And sets it up
def create
# Get User by username...
email = params[:user][:email]
@user = User.where(:email => email).first
@employee = Employee.create(params[:employee])
if @user.nil? # assume true
flash[:error] = "Error: User #{email} not found or something. Hmmm..."
@user = User.create(:email => email) # setup failing user object so it gets to view
render :action => :index #"index" # "/employees/index.html.erb"
return
else
.
.
.
(views/employees/index.html.erb)
<h1>From this page, you can set users to 'employee' status</h1> <br> <p>Giving a user employee status, allows them to be able to see more data than regular customers can see. </p> <!-- When the user1.employee == nil, they are considered normal users. But when you click Make Employee here, it will bring you to a form where you fill in their Employee settings. --> <h1> Make New Employee </h1> <br> <%= render 'emp_form' %> <h1> Remove Employee </h1> User Name:<input type="text"></input><br> <br><br> <a href="#">Make Normal User</a>
(views/employees/_emp_form.html.erb)
<%= form_for(@employee, :url => { :action => "create", :controller => "employees" } ) do |f| %>
<% if @employee.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@employee.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @employee.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= label 'user', 'email', 'User Login email (user must have created account already)' %><br />
<%= text_field(:user, :email) %>
</div>
<div class="field">
<%= f.label :employeeid, 'Employee ID (workpro)' %><br />
<%= f.text_field :employeeid %>
</div>
<div class="field">
<%= f.label :position_title %><br />
<%= f.text_field :position_title %>
</div>
<div class="field">
<%= f.label 'Date of Birth' %><br />
<%= f.text_field :dob %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
(routes.rb)
resources :employees
This post has been edited by NotarySojac: 17 December 2011 - 09:36 AM

New Topic/Question
Reply




MultiQuote




|