Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turbo frames: inline editing #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/employees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/employee.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 9 additions & 30 deletions app/views/employees/_employee.html.erb
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
<div id="<%= dom_id employee %>">
<p>
<strong>First name:</strong>
<%= employee.first_name %>
</p>

<p>
<strong>Last name:</strong>
<%= employee.last_name %>
</p>

<p>
<strong>Dob:</strong>
<%= employee.dob %>
</p>

<p>
<strong>Country:</strong>
<%= employee.country %>
</p>

<p>
<strong>Phone:</strong>
<%= employee.phone %>
</p>

<p>
<strong>Email:</strong>
<%= employee.email %>
</p>

<%# [:first_name, :last_name, :dob, :country, :phone, :email].each do |attribute| %>
<% Employee::INLINE_EDITABLE_ATTRS.each do |attribute| %>
<%= turbo_frame_tag attribute do %>
<p>
<strong><%= attribute %>:</strong>
<%= link_to (employee[attribute].presence || 'Edit'), [:edit, employee, attribute: attribute] %>
</p>
<% end %>
<% end %>
</div>
46 changes: 46 additions & 0 deletions app/views/employees/_inline_attribute_form.html.erb
Original file line number Diff line number Diff line change
@@ -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' %>
<div>
<%= form.label :first_name, style: "display: block" %>
<%= form.text_field :first_name, onfocusout: 'this.form.requestSubmit()' %>
</div>
<% end %>

<% if params[:attribute].eql? 'last_name' %>
<div>
<%= form.label :last_name, style: "display: block" %>
<%= form.text_field :last_name, onfocusout: 'this.form.requestSubmit()' %>
</div>
<% end %>

<% if params[:attribute].eql? 'dob' %>
<div>
<%= form.label :dob, style: "display: block" %>
<%= form.datetime_field :dob, onfocusout: 'this.form.requestSubmit()' %>
</div>
<% end %>

<% if params[:attribute].eql? 'country' %>
<div>
<%= form.label :country, style: "display: block" %>
<%= form.text_field :country, onfocusout: 'this.form.requestSubmit()' %>
</div>
<% end %>

<% if params[:attribute].eql? 'phone' %>
<div>
<%= form.label :phone, style: "display: block" %>
<%= form.text_field :phone, onfocusout: 'this.form.requestSubmit()' %>
</div>
<% end %>

<% if params[:attribute].eql? 'email' %>
<div>
<%= form.label :email, style: "display: block" %>
<%= form.text_field :email, onfocusout: 'this.form.requestSubmit()' %>
</div>
<% end %>
<% end %>
<% end %>
6 changes: 5 additions & 1 deletion app/views/employees/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<h1>Editing employee</h1>

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

<br>

Expand Down