Skip to content

Instantly share code, notes, and snippets.

@universal
Forked from ryanvermooten/_form.html.erb
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save universal/6d872701e98854e08252 to your computer and use it in GitHub Desktop.

Select an option

Save universal/6d872701e98854e08252 to your computer and use it in GitHub Desktop.
<%= form_for Group.new do |f| %>
<table summary = "groups from trainer">
<tr>
<th><%= f.label(:name) %></th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th><%= f.label(:area) %></th>
<td><%= f.text_field(:area) %></td>
</tr>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</table>
class GroupsController < ApplicationController
#before_action :set_group, only: [:show, :edit, :update, :destroy]
before_action :set_trainer
# GET /trainers/1/groups
# GET /trainers/1/groups.json
def index
@groups = Group.all
end
# GET /trainers/1/groups/1
# GET /trainers/1/groups/1.json
def show
end
# GET /trainers/1/groups/new
def new
@trainer = Trainer.find(params[:trainer_id])
end
# GET /trainers/1/groups/1/edit
def edit
end
# POST /trainers/1/groups
# POST /trainers/1/groups.json
def create
@group = Group.new(group_params)
respond_to do |format|
if @group.save
format.html { redirect_to @group, notice: 'Group was successfully created.' }
format.json { render action: 'show', status: :created, location: @group }
else
format.html { render action: 'new' }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /trainers/1/groups/1
# PATCH/PUT /trainers/1/groups/1.json
def update
respond_to do |format|
if @group.update(group_params)
format.html { redirect_to @group, notice: 'Group was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
# DELETE /trainers/1/groups/1
# DELETE /trainers/1/groups/1.json
def destroy
@group.destroy
respond_to do |format|
format.html { redirect_to groups_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_group
@group = Group.find(params[:id])
end
def set_trainer
@trainer = Trainer.find(params[:trainer_id])
end
def find_trainer
if params[:trainer_id]
@trainer = Trainer.find(params[:trainer_id]).permit(:trainer_id)
end
# Never trust parameters from the scary internet, only allow the white list through.
def group_params
params.require(:group).permit(:name, :area, :trainer_id)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment