Implemented UnitsController#create/destroy
This commit is contained in:
parent
e9fc9c6475
commit
b503f91e3b
@ -1,5 +1,6 @@
|
|||||||
class UnitsController < ApplicationController
|
class UnitsController < ApplicationController
|
||||||
before_action :find_project, only: [:index, :create, :import]
|
before_action :find_project, only: [:index, :create, :import]
|
||||||
|
before_action :find_unit, only: [:destroy]
|
||||||
before_action :authorize
|
before_action :authorize
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@ -8,9 +9,21 @@ class UnitsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
@unit = Unit.new(unit_params.update(project: @project))
|
||||||
|
if @unit.save
|
||||||
|
flash[:notice] = 'Created new unit'
|
||||||
|
redirect_to project_units_url(@project)
|
||||||
|
else
|
||||||
|
@units = @project.units
|
||||||
|
render :index
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
if @unit.destroy
|
||||||
|
flash[:notice] = 'Deleted unit'
|
||||||
|
end
|
||||||
|
redirect_to project_units_url(@project)
|
||||||
end
|
end
|
||||||
|
|
||||||
def import
|
def import
|
||||||
@ -23,6 +36,13 @@ class UnitsController < ApplicationController
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def unit_params
|
||||||
|
params.require(:unit).permit(
|
||||||
|
:name,
|
||||||
|
:shortname
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
# :find_* methods are called before :authorize,
|
# :find_* methods are called before :authorize,
|
||||||
# @project is required for :authorize to succeed
|
# @project is required for :authorize to succeed
|
||||||
def find_project
|
def find_project
|
||||||
@ -30,4 +50,11 @@ class UnitsController < ApplicationController
|
|||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
render_404
|
render_404
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_unit
|
||||||
|
@unit = Unit.find(params[:id])
|
||||||
|
@project = @unit.project
|
||||||
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
render_404
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -11,12 +11,12 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="add-unit" style="display:none;">
|
<div id="add-unit" <%= 'style=display:none;' if @unit.errors.empty? %>>
|
||||||
<h2><%= t ".heading_new_unit" %></h2>
|
<h2><%= t ".heading_new_unit" %></h2>
|
||||||
|
|
||||||
<%= labelled_form_for @unit,
|
<%= labelled_form_for @unit,
|
||||||
:url => project_units_path(@project),
|
:url => project_units_path(@project),
|
||||||
:html => { :id => 'unit-form', :multipart => true } do |f| %>
|
:html => {:id => 'unit-form'} do |f| %>
|
||||||
<%= render :partial => 'units/form', :locals => { :f => f } %>
|
<%= render :partial => 'units/form', :locals => { :f => f } %>
|
||||||
<%= submit_tag l(:button_create) %>
|
<%= submit_tag l(:button_create) %>
|
||||||
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-unit").hide()' %>
|
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-unit").hide()' %>
|
||||||
@ -40,7 +40,7 @@
|
|||||||
<td class="shortname"><%= u.shortname %></td>
|
<td class="shortname"><%= u.shortname %></td>
|
||||||
<td class="unitname"><%= u.name %></td>
|
<td class="unitname"><%= u.name %></td>
|
||||||
<td>
|
<td>
|
||||||
<%= delete_link unit_path(u), :remote => true %>
|
<%= delete_link unit_path(u) %>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
Reference in New Issue
Block a user