-
-
Save codeschool-courses/1173997 to your computer and use it in GitHub Desktop.
Rails for Zombies 2 – Challenge 5-9
This file contains hidden or 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
<p id="notice"><%= notice %></p> | |
<ul> | |
<li> | |
<em>Name:</em> | |
<%= @weapon.name %> | |
</li> | |
<li> | |
<em>Condition:</em> | |
<span id="condition"><%= @weapon.condition %></span> | |
<%= link_to "Toggle", toggle_condition_user_weapon_path(@user, @weapon), remote: true %> | |
</li> | |
<li> | |
<em>Ammo:</em> | |
<span id="ammo"><%= @weapon.ammo %></span> | |
</li> | |
</ul> | |
<div id="reload_form"> | |
<%= form_for [@user, @weapon], url: reload_user_weapon_path(@user, @weapon), remote:true do |f| %> | |
<div class="field"> | |
Number of bullets to reload: | |
<%= number_field_tag :ammo_to_reload, 30 %> <br /> | |
<%= f.submit "Reload" %> | |
</div> | |
<% end %> | |
</div> | |
<%= link_to 'Edit', edit_weapon_path(@weapon) %> | | |
<%= link_to 'Back', weapons_path %> |
This file contains hidden or 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 WeaponsController < ApplicationController | |
def reload | |
@weapon = Weapon.find(params[:id]) | |
respond_to do |format| | |
if @weapon.ammo < 30 | |
@weapon.reload(params[:ammo_to_reload]) | |
format.json { render :json => @weapon.to_json(:only => :ammo), status: :ok } | |
format.html { redirect_to @weapon, notice: 'Weapon ammo reloaded' } | |
else | |
format.json { render :json => @weapon.to_json(:only => :ammo), status: :unprocessable_entity } | |
format.html { redirect_to @weapon, notice: 'Weapon not reloaded' } | |
end | |
format.js | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment