Action | HTTP method | URL | 生成URL的方法 | |
1 | index | GET | /exercises | exercises_path |
2 | show | GET | /exercises/1 | exercises(:id) |
3 | new | GET | /exercises/new | new_exercise_path |
4 | edit | GET | /exercises/1;edit | edit_exercise_path(:id) |
5 | create | POST | /exercises | exercises_path |
6 | update | PUT | /exercises/1 | exercises_path |
7 | destroy | DELETE | /exercises/1 | exercises_path(:id) |
map.resources :exercises
map.resources :users, :sessions
map.signup '/signup', :controller=>'users', :action=>'new'
map.logout '/logout', :controller=>'sessions', :action=>'destory'
end
"[url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title><%=@title || "Exercisr"%></title>
<link rel="stylesheet" type="text/css" href="[url]http://yui.yahooapis.com/2.2.2/build/reset-fonts-grids/reset-fonts-grids.css[/url]">
<%= stylesheet_link_tag 'styles' %>
<%= javascript_include_tag :defaults %>
</head>
<body>
<div id="doc2" class="yui-t2">
<div id="hd" class="box grad blue">
<%= p_w_picpath_tag 'grad_black.png'%>
<h1 id="masthead"><%= link_to "Exercisr", home_path%></h1>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<%= yield%>
</div>
</div>
<%if logged_in?%>
<div class="yui-b sidebar">
<ul>
<li><%=link_to 'Exercises', exercises_path%></li>
<li><%# link_to 'Workouts', workouts_path%></li>
<li><%# link_to 'Goals', goals_path%></li>
<li><%# link_to 'Logout', logout_path%></li>
</ul>
</div>
<div class="yui-b sidebar">
<ul>
<li><%= link_to 'signup', signup_path%></li>
</ul>
</div>
<%end%>
</div>
<div id="ft" class="box grad blue"><%= p_w_picpath_tag 'grad_white.png'%></div>
</div>
</body>
</html>
<h3>A RESTful place to keep track of your workouts</h3>
map.resources :exercises
map.resources :users, :sessions
map.welcome '/welcome', :controller=>'sessions', :action=>'welcome'
map.signup '/signup', :controller=>'users', :action=>'new'
map.login '/login', :controller=>'sessions', :action=>'new'
map.logout '/logout', :controller=>'sessions', :action=>'destory'
end
...
...
...
redirect_back_or_default(login_path)
end
redirect_back_or_default(welcome_path)
end
belongs_to :user
validates_presence_of :name
validates_uniqueness_of :name, :scope=>:user_id #一个用户不能输入两个相同的运动名
end
# ...
has_many :exercises, :dependent => :destroy, :order=>'name asc'
end
before_filter :login_required
# GET /exercises
# GET /exercises.xml
def index
@exercises =current_user.exercises.find(:all)
format.html # index.rhtml
format.xml { render :xml => @exercises.to_xml }
end
end
# GET /exercises/1.xml
def show
@exercise =current_user.exercises.find(params[:id])
format.html # show.rhtml
format.xml { render :xml => @exercise.to_xml }
end
end
def new
@exercise = current_user.exercises.build
end
def edit
@exercise = current_user.exercises.find(params[:id])
end
# POST /exercises.xml
def create
@exercise = current_user.exercises.build(params[:exercise])
if @exercise.save
flash[:notice] = 'Exercise was successfully created.'
format.html { redirect_to exercise_url(@exercise) }
format.xml { head :created, :location => exercise_url(@exercise) }
else
format.html { render :action => "new" }
format.xml { render :xml => @exercise.errors.to_xml }
end
end
end
# PUT /exercises/1.xml
def update
@exercise = current_user.exercises.find(params[:id])
if @exercise.update_attributes(params[:exercise])
flash[:notice] = 'Exercise was successfully updated.'
format.html { redirect_to exercise_url(@exercise) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @exercise.errors.to_xml }
end
end
end
# DELETE /exercises/1.xml
def destroy
@exercise = current_user.exercises.find(params[:id])
@exercise.destroy
format.html { redirect_to exercises_url }
format.xml { head :ok }
end
end
end
<label for="exercise-name">Name</label><br />
<%= f.text_field :name %>
</p>
<%= submit_tag "Save" %>
</p>
<%= render :partial=>"form", :locals=>{:f=>f}%>
<% end %>
<%=render :partial=>'form', :locals=>{:f=>f}%>
<% end %>
<%= link_to 'Back', exercises_path %>
<p>On this page you can create and manage the exercises that you use in your workouts.</p>
<p>You can also view reports on your progress for each exercises</p>
<tr>
<th>Name</th>
</tr>
<%= render :partial=>'exercise', :collection=>@exercises%>
</table>
<h1>Add a New Exercise</h1>
<div id="add_exercise">
<% form_for(:exercise, :url=>exercises_path, :html=>{:id=>'new_exercise'}) do |f|%>
<%=render :partial=>'form', :locals=>{:f=>f}%>
<%end%>
</div>
before_filter :login_required
# GET /workouts
# GET /workouts.xml
def index
@workouts = current_user.workouts.find(:all)
format.html # index.rhtml
format.xml { render :xml => @workouts.to_xml }
end
end
# GET /workouts/1.xml
def show
@workout = current_user.workouts.find(params[:id])
format.html # show.rhtml
format.xml { render :xml => @workout.to_xml }
end
end
def new
@workout = current_user.workouts.build
end
def edit
@workout = current_user.workouts.find(params[:id])
end
# POST /workouts.xml
def create
@workout = current_user.workouts.build(params[:workout])
if @workout.save
flash[:notice] = 'Workout was successfully created.'
format.html { redirect_to workout_url(@workout) }
format.xml { head :created, :location => workout_url(@workout) }
else
format.html { render :action => "new" }
format.xml { render :xml => @workout.errors.to_xml }
end
end
end
# PUT /workouts/1.xml
def update
@workout = current_user.workouts.find(params[:id])
if @workout.update_attributes(params[:workout])
flash[:notice] = 'Workout was successfully updated.'
format.html { redirect_to workout_url(@workout) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @workout.errors.to_xml }
end
end
end
# DELETE /workouts/1.xml
def destroy
@workout = current_user.workouts.find(params[:id])
@workout.destroy
format.html { redirect_to workouts_url }
format.xml { head :ok }
end
end
end
<tr>
<th>Date</th>
<th>Label</th>
</tr>
<%= render :partial=>'workout', :collection=>@workouts %>
</table>
<div id="add_workout">
<%form_for(:workout, :url=>workouts_path, :html=>{:id=>'new_workout'}) do |f|%>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<%end%>
</div>
<td><%=h workout.date.to_s(:long) %></td>
<td><%=h workout.label %></td>
<td><%= link_to p_w_picpath_tag("edit_photo.gif", {:title=>"Edit Workout Date/label"}), edit_workout_path(workout) %></td>
<td><%= link_to p_w_picpath_tag("delete_photo.gif", {:title=>"Delete Workout"}), workout_path(workout), :confirm => 'Are you sure?', :method => :delete %></td>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<% end %>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<% end %>
<%= link_to 'Back', workouts_path %>
<b>Date</b><br />
<%= f.date_select :date %>
</p>
<b>Label</b><br />
<%= f.text_field :label %>
</p>
<%= submit_tag "Save" %>
</p>
belongs_to :exercise
belongs_to :workout
validates_presence_of :resistance, :repetitions
end
belongs_to :user
has_many :activities, :dependent=>:destroy
has_many :exercises, :through=>:activities
validates_presence_of :date
end
# Virtual attribute for the unencrypted password
has_many :exercises, :dependent => :destroy, :order=>'name asc'
has_many :workouts, :dependent => :destroy
has_many :activities, :through=>:workouts
# map.resources :activities
workout.resources :activities
end
def find_workout
@workout= current_user.workouts.find(params[:workout_id])
end
before_filter :login_required
before_filter :find_workout
# GET /activities
# GET /activities.xml
def index
@activities = @workout.activities.find(:all)
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @activities.to_xml }
end
end
# GET /activities/1.xml
def show
@activity = @workout.activities.find(params[:id])
format.html # show.rhtml
format.xml { render :xml => @activity.to_xml }
end
end
def new
@activity = @workout.activities.build
end
def edit
@activity = @workout.activities.find(params[:id])
end
# POST /activities.xml
@activity = @workout.activities.build(params[:activity])
if @activity.save
flash[:notice] = 'Activity was successfully created.'
format.html { redirect_to workout_path(@workout) }
format.xml { head :created, :location => activity_url(@workout,@activity) }
else
format.html { render :action => "new" }
format.xml { render :xml => @activity.errors.to_xml }
end
end
end
# PUT /activities/1.xml
@activity = @workout.activities.find(params[:id])
if @activity.update_attributes(params[:activity])
flash[:notice] = 'Activity was successfully updated.'
format.html { redirect_to workout_path(@workout) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @activity.errors.to_xml }
end
end
end
# DELETE /activities/1.xml
def destroy
@activity = @workout.activities.find(params[:id])
@activity.destroy
format.html { redirect_to activities_url }
format.xml { head :ok }
end
end
def find_workout
@workout= current_user.workouts.find(params[:workout_id])
end
<td><%=h activity.exercise.name %></td>
<td><%= link_to p_w_picpath_tag("delete_photo.gif"), activity_path(@workout,activity), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<%= f.collection_select :exercise_id, current_user.exercises.find(:all), :id, :name, :prompt=>"Select an Exercise" %>
</p>
</p>
<%= submit_tag "Save" %>
</p>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<% end %>
<tr>
<th>Exercise</th>
<th>Resistance</th>
<th>Repetitions</th>
</tr>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<% end %>
<b>Exercise:</b>
<%=h @activity.exercise %>
</p>
<b>Resistance:</b>
<%=h @activity.resistance %>
</p>
<b>Repetitions:</b>
<%=h @activity.repetitions %>
</p>
<%= link_to 'Back', activities_path(@workout) %>
@workout = current_user.workouts.find(params[:id])
@activities= @workout.activities.find(:all, :include=>:exercise)
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @workout.to_xml }
end
end
<table>
<tr><th>Exercise</th><th>Reps</th><th>Resistance</th></tr>
<%=render :partial=>'activities/activity', :collection=>@activities%>
</table>
<%form_for(:activity, :url=>activities_path(@workout)) do |f|%>
<%= render :partial=>'activities/form', :locals=>{:f=>f}%>
<%end%>
<%=link_to "Back", workouts_path%>
<%= f.collection_select :exercise_id, current_user.exercises.find(:all), :id, :name, :prompt=>"Select an Exercise" %>
or add a new exercise:
<%= f.text_field :new_exercise_name%>
</p>
</p>
<%= submit_tag "Save" %>
</p>
belongs_to :exercise
belongs_to :workout
validates_presence_of :resistance, :repetitions
attr_accessor :new_exercise_name
before_save :create_exercise_if_submitted
def create_exercise_if_submitted
create_exercise(:user_id=>workout.user_id,
:name=>new_exercise_name) unless new_exercise_name.blank?
end
end
belongs_to :user
has_many :results, :dependent => :destroy
validates_presence_of :value
end
belongs_to :goal
validates_presence_of :date, :value
end
...
has_many :goals
# map.resources :results
goal.resources :results
end
before_filter :login_required
# GET /goals
# GET /goals.xml
def index
@goals = current_user.goals.find(:all)
format.html # index.rhtml
format.xml { render :xml => @goals.to_xml }
end
end
# GET /goals/1.xml
def show
@goal = current_user.goals.find(params[:id])
@results=@goal.results.find(:all, :order=>'date desc')
format.html # show.rhtml
format.xml { render :xml => @goal.to_xml }
end
end
def new
@goal = current_user.goals.build
end
def edit
@goal = current_user.goals.find(params[:id])
end
# POST /goals.xml
def create
@goal = current_user.goals.build(params[:goal])
if @goal.save
flash[:notice] = 'Goal was successfully created.'
format.html { redirect_to goal_url(@goal) }
format.xml { head :created, :location => goal_url(@goal) }
else
format.html { render :action => "new" }
format.xml { render :xml => @goal.errors.to_xml }
end
end
end
# PUT /goals/1.xml
def update
@goal = current_user.goals.find(params[:id])
if @goal.update_attributes(params[:goal])
flash[:notice] = 'Goal was successfully updated.'
format.html { redirect_to goal_url(@goal) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @goal.errors.to_xml }
end
end
end
# DELETE /goals/1.xml
def destroy
@goal = current_user.goals.find(params[:id])
@goal.destroy
format.html { redirect_to goals_url }
format.xml { head :ok }
end
end
end
# GET /results
# GET /results.xml
before_filter :login_required
before_filter :find_goal
def index
@results = @goal.results.find(:all)
format.html # index.rhtml
format.xml { render :xml => @results.to_xml }
end
end
# GET /results/1.xml
def show
@result = @goal.results.find(params[:id])
format.html # show.rhtml
format.xml { render :xml => @result.to_xml }
end
end
def new
@result = @goal.results.build
end
def edit
@result = @goal.results.find(params[:id])
end
# POST /results.xml
def create
@result = @goal.results.build(params[:result])
if @result.save
flash[:notice] = 'Result was successfully created.'
format.html { redirect_to goal_url(@goal) }
format.xml { head :created, :location => result_url(@result) }
else
format.html { render :action => "new" }
format.xml { render :xml => @result.errors.to_xml }
end
end
end
# PUT /results/1.xml
def update
@result = @goal.results.find(params[:id])
if @result.update_attributes(params[:result])
flash[:notice] = 'Result was successfully updated.'
format.html { redirect_to goal_url(@goal) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @result.errors.to_xml }
end
end
end
# DELETE /results/1.xml
def destroy
@result = @goal.results.find(params[:id])
@result.destroy
format.html { redirect_to goal_url(@goal) }
format.xml { head :ok }
end
end
protected
def find_goal
@goal=current_user.goals.find(params[:goal_id])
end
end
<label for="goal-name">Name of the Goal:</label><br />
<%= f.text_field :name %>
</p>
<label for="goal-value">Goal to Reach:</label><br />
<%= f.text_field :value %>
</p>
<label for="goal-last">Current Result:</label><br />
<%= f.text_field :last %>
</p>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<% end %>
<%= link_to 'Back', goals_path %>
<%= render :partial=>"form", :locals=>{:f=>f}%>
<% end %>
<td><%=h goal.name %></td>
<td></td>
<td><%= link_to p_w_picpath_tag('edit_photo.gif',{:title=>"Edit Goal Details"}), edit_goal_path(goal) %></td>
<td><%= link_to p_w_picpath_tag("delete_photo.gif"), goal_path(goal), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<tr>
<th>Name</th>
</tr>
<%=render :partial=>"goal", :collection=>@goals%>
</table>
<h1>Add a New Goal</h1>
<div id="add_goal">
<% form_for(:goal, :url=>goals_path, :html=>{:id=>'new_goal'}) do |f|%>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<%end%>
</div>
<label for="">Date</label><br />
<%= f.date_select :date %>
</p>
<label for="">Value</label><br />
<%= f.text_field :value %>
</p>
<%= submit_tag "Save" %>
</p>
<td><%=h result.date.to_s(:long) %></td>
<td><%=h result.value %></td>
<td><%= link_to p_w_picpath_tag("delete_photo.gif",{:title=>"Delete Result"}), result_path(@goal,result), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<%= render :partial=>'form', :locals=>{:f=>f}%>
<% end %>
<%= render :partial=>"form", :locals=>{:f=>f}%>
<% end %>
<tr>
<th>Value</th>
</tr>
<%= link_to "Back to Goal", goal_path(@goal)%>
<table>
<tr><th>Date</th><th>Value</th></tr>
<%= render :partial=>'results/result', :collection=>@results%>
</table>
<% form_for :result, :url=>results_path(@goal) do |f|%>
<%= render :partial=>'results/form', :locals=>{:f=>f}%>
<%end%>
<%= link_to 'Back', workouts_path%>
belongs_to :goal
validates_presence_of :date, :value
after_create :update_last_result
def update_last_result
goal.last=value
goal.save
end
end
上一篇:云计算的基本原理和概念
-
探索Django REST框架构建强大的API
探讨了Django中REST框架的一系列功能和技术,涵盖了API开发中的各个方面。
REST Django API开发 RESTful API -
SerfJ REST框架的示例代码
[1].[代码] web.xml 01<servlet>02 <servlet-name>RestServl
Rest SerfJ html Code java
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M