Skip to content

Instantly share code, notes, and snippets.

@ytsarev
Forked from codeschool-courses/show.html.erb
Created February 22, 2012 23:48
Show Gist options
  • Save ytsarev/1888453 to your computer and use it in GitHub Desktop.
Save ytsarev/1888453 to your computer and use it in GitHub Desktop.
Rails for Zombies 2 – Challenge 5-9
<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 %>
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