日期:2014-05-16 浏览次数:20395 次
app/views/layouts/tasks.rhtml: <html> <head> <title>Tasks: <%= controller.action_name %></title> <%= javascript_include_tag :defaults %> <%= stylesheet_link_tag 'scaffold' %> </head> <body> <p style="color: green"><%= flash[:notice] %></p> <%= yield %> </body> </html>通过载入 一个局部模版,index视图中显示了任务列表,
app/views/tasks/index.rhtml: <h1>My Tasks</h1> <div id="notice"></div> <div id="task_list"> <%= render :partial => 'list' %> </div> <br /> <% form_remote_tag :url => {:action => 'add_task'} do %> <p><label for="task_name">Add Task</label>; <%= text_field 'task', 'name' %></p> <%= submit_tag "Create" %> <% end %>
app/views/tasks/_list.rhtml: <ul> <% for task in @tasks %> <% for column in Task.content_columns %> <li><%=h task.send(column.name) %></li> <% end %> <% end %> </ul>接着, 为了显示和添加的恶任务,定义两个方法 index和add_task。
app/controllers/tasks_controller.rb: class TasksController < ApplicationController def index @tasks = Task.find :all end def add_task @task = Task.new(params[:task]) @task.save @tasks = Task.find :all end end
app/views/tasks/add_task.rjs: page.replace_html 'notice', "<span style=\"color: green;\">#{@tasks.length} tasks, updated on #{Time.now}</span>" page.replace_html 'task_list', :partial => 'list' page.visual_effect :highlight, 'task_list', :duration => 4[/color][/size]