Last active
August 29, 2015 14:12
-
-
Save josephrexme/92722fcc9ac9d6b1201e to your computer and use it in GitHub Desktop.
Multiple CRUD operations on a page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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