1
0

Implemented UnitsController#create/destroy

This commit is contained in:
cryptogopher 2019-08-22 17:47:32 +02:00
parent e9fc9c6475
commit b503f91e3b
2 changed files with 30 additions and 3 deletions

View File

@ -1,5 +1,6 @@
class UnitsController < ApplicationController
before_action :find_project, only: [:index, :create, :import]
before_action :find_unit, only: [:destroy]
before_action :authorize
def index
@ -8,9 +9,21 @@ class UnitsController < ApplicationController
end
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
def destroy
if @unit.destroy
flash[:notice] = 'Deleted unit'
end
redirect_to project_units_url(@project)
end
def import
@ -23,6 +36,13 @@ class UnitsController < ApplicationController
private
def unit_params
params.require(:unit).permit(
:name,
:shortname
)
end
# :find_* methods are called before :authorize,
# @project is required for :authorize to succeed
def find_project
@ -30,4 +50,11 @@ class UnitsController < ApplicationController
rescue ActiveRecord::RecordNotFound
render_404
end
def find_unit
@unit = Unit.find(params[:id])
@project = @unit.project
rescue ActiveRecord::RecordNotFound
render_404
end
end

View File

@ -11,12 +11,12 @@
<% end %>
</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>
<%= labelled_form_for @unit,
: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 } %>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-unit").hide()' %>
@ -40,7 +40,7 @@
<td class="shortname"><%= u.shortname %></td>
<td class="unitname"><%= u.name %></td>
<td>
<%= delete_link unit_path(u), :remote => true %>
<%= delete_link unit_path(u) %>
</td>
</tr>
<% end %>