diff --git a/app/controllers/employees_controller.rb b/app/controllers/employees_controller.rb index 0fccff1..170924b 100644 --- a/app/controllers/employees_controller.rb +++ b/app/controllers/employees_controller.rb @@ -3,7 +3,7 @@ class EmployeesController < ApplicationController # GET /employees or /employees.json def index - @employees = Employee.all + @employees = Employee.order(:created_at) end # GET /employees/1 or /employees/1.json diff --git a/app/models/employee.rb b/app/models/employee.rb index 7051db4..361df96 100644 --- a/app/models/employee.rb +++ b/app/models/employee.rb @@ -1,3 +1,5 @@ class Employee < ApplicationRecord + INLINE_EDITABLE_ATTRS = [:first_name, :last_name, :dob, :country, :phone, :email] + validates :first_name, :last_name, :email, presence: true end diff --git a/app/views/employees/_employee.html.erb b/app/views/employees/_employee.html.erb index 8eb9ef0..e723f95 100644 --- a/app/views/employees/_employee.html.erb +++ b/app/views/employees/_employee.html.erb @@ -1,32 +1,11 @@
-

- First name: - <%= employee.first_name %> -

- -

- Last name: - <%= employee.last_name %> -

- -

- Dob: - <%= employee.dob %> -

- -

- Country: - <%= employee.country %> -

- -

- Phone: - <%= employee.phone %> -

- -

- Email: - <%= employee.email %> -

- + <%# [:first_name, :last_name, :dob, :country, :phone, :email].each do |attribute| %> + <% Employee::INLINE_EDITABLE_ATTRS.each do |attribute| %> + <%= turbo_frame_tag attribute do %> +

+ <%= attribute %>: + <%= link_to (employee[attribute].presence || 'Edit'), [:edit, employee, attribute: attribute] %> +

+ <% end %> + <% end %>
diff --git a/app/views/employees/_inline_attribute_form.html.erb b/app/views/employees/_inline_attribute_form.html.erb new file mode 100644 index 0000000..b995201 --- /dev/null +++ b/app/views/employees/_inline_attribute_form.html.erb @@ -0,0 +1,46 @@ +<%= turbo_frame_tag params[:attribute] do %> + <%= form_with(model: employee, url: [employee, attribute: params[:attribute]], method: :patch) do |form| %> + + <% if params[:attribute].eql? 'first_name' %> +
+ <%= form.label :first_name, style: "display: block" %> + <%= form.text_field :first_name, onfocusout: 'this.form.requestSubmit()' %> +
+ <% end %> + + <% if params[:attribute].eql? 'last_name' %> +
+ <%= form.label :last_name, style: "display: block" %> + <%= form.text_field :last_name, onfocusout: 'this.form.requestSubmit()' %> +
+ <% end %> + + <% if params[:attribute].eql? 'dob' %> +
+ <%= form.label :dob, style: "display: block" %> + <%= form.datetime_field :dob, onfocusout: 'this.form.requestSubmit()' %> +
+ <% end %> + + <% if params[:attribute].eql? 'country' %> +
+ <%= form.label :country, style: "display: block" %> + <%= form.text_field :country, onfocusout: 'this.form.requestSubmit()' %> +
+ <% end %> + + <% if params[:attribute].eql? 'phone' %> +
+ <%= form.label :phone, style: "display: block" %> + <%= form.text_field :phone, onfocusout: 'this.form.requestSubmit()' %> +
+ <% end %> + + <% if params[:attribute].eql? 'email' %> +
+ <%= form.label :email, style: "display: block" %> + <%= form.text_field :email, onfocusout: 'this.form.requestSubmit()' %> +
+ <% end %> + <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/employees/edit.html.erb b/app/views/employees/edit.html.erb index 5795f0c..e95c7ed 100644 --- a/app/views/employees/edit.html.erb +++ b/app/views/employees/edit.html.erb @@ -1,6 +1,10 @@

Editing employee

-<%= render "form", employee: @employee %> +<% if params[:attribute].present? %> + <%= render "inline_attribute_form", employee: @employee %> +<% else %> + <%= render "form", employee: @employee %> +<% end %>