Skip to content

Instantly share code, notes, and snippets.

@josephrexme
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephrexme/92722fcc9ac9d6b1201e to your computer and use it in GitHub Desktop.
Save josephrexme/92722fcc9ac9d6b1201e to your computer and use it in GitHub Desktop.
Multiple CRUD operations on a page
<!-- Create -->
<%= form_for @department do |f| %>
<p>
<%= f.label :name, 'Department Name' %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :parent %>
<% department_array = @departments.all.map { |department| [department.name, department.name] } %>
<%= f.select :parent, options_for_select([['no parent', '']] + department_array) %>
</p>
<%= f.submit 'Add Department', class: 'btn-std' %>
<% end %>
<!-- In the same file for updtate -->
<%= form_for @department do |f| %>
<%= f.label 'Department Name' %>
<%= f.text_field :name, value: "#{department.name}" %>
<%= f.label :Parent %>
<% department_array = @departments.all.map { |dept| [dept.name, dept.name] } %>
<%= f.select :parent, options_for_select([['no parent', '']] + department_array, "#{department.parent}") %>
<%= f.submit :update, class: 'btn-edit' %>
<% end %>
class DashboardController < ApplicationController
before_action :require_login
before_filter :init
def index
@departments.each do |department|
childfetch = Department.where(parent: "#{department.name}").select('name')
instance_variable_set("@#{department.name}Children".gsub(' ','_'), childfetch.map { |child| "#{child.name}" }.join(',') )
end
end
def attendance
end
def preferences
end
def company
@department = Department.new
@allDepartments = Department.all
#render text: 'foo' and return
end
private
def require_login
unless session[:user_id].present?
redirect_to root_url
end
end
def init
@departments = Department.where(parent: '')
end
end
class DepartmentsController < ApplicationController
def index
end
def create
@department = Department.new(department_params)
if @department.save
redirect_to :back, notice: 'Department created'
else
redirect_to dashboard_path
end
end
def edit
end
def update
@department = Department.new(department_params)
# update should come here
render text: @department and return
end
def destroy
end
private
def department_params
params.require(:department).permit(:name, :parent)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment