今天来看看用Rails创建一个可拖拽的List。
1,创建Rails项目,搭建数据库
Migration:
class AddPersonAndGroceryListsAndFoodItemsTables < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.column :name, :string
end
create_table :grocery_lists do |t|
t.column :name, :string
t.column :person_id, :integer
end
create_table :food_items do |t|
t.column :grocery_list_id, :integer
t.column :position, :integer
t.column :name, :string
t.column :quantity, :integer
end
end
def self.down
drop_table :people
drop_table :grocery_lists
drop_table :food_items
end
end
Model:
class Person < ActiveRecord::Base
has_many :grocery_lists
end
class GroceryList < ActiveRecord::Base
has_many :food_items, :order => :position
belongs_to :person
end
class FoodItem < ActiveRecord::Base
belongs_to :grocery_list
acts_as_list :scope => :grocery_list
end
这里一个Person对应多个GroceryList,一个GroceryList对应多个FoodItem
我们可以运行
ruby script/console或者直接访问数据库来初始化几条数据
这里我们创建一个Person,一个GroceryList和3个FoodItem,并保持外键关联
2,引入javascript
可以通过用一个layout文件来做这件事,如app/views/layouts/standard.rhtml:
<html>
<head>
<%= javascript_include_tag :defaults %>
</head>
<body>
<%= yield %>
</body>
</html>
3,创建一个列表controller
如app/controllers/grocery_list_controller.rb:
class GroceryListController < ApplicationController
layout 'standard'
def show
@grocery_list = GroceryList.find(params[:id])
end
def sort
@grocery_list = GroceryList.find(params[:id])
@grocery_list.food_items.each do |food_item|
food_item.position = params['grocery-list'].index(food_item.id.to_s) + 1
print food_item.position
food_item.save
end
render :noting => true
end
end
4,创建列表视图
如app/views/grocery_list/show.rhtml:
<h2><%= @grocery_list.person.name %>'s Grocery List</h2>
<h3><%= @grocery_list.name %></h3>
<ul id="grocery-list">
<% @grocery_list.food_items.each do |food_item| %>
<li id="item_<%= food_item.id %>">
<%= food_item.quantity %> units of <%= food_item.name %>
</li>
<% end %>
</ul>
<%= sortable_element 'grocery-list',
:url => { :action => "sort", :id => @grocery_list },
:complete => visual_effect(:highlight, 'grocery-list')
%>
5,访问看看效果
打开浏览器访问
http://localhost:3000/grocery_list/show/1看看
我们拖拽一条数据在列表中的位置,然后刷新页面,发现该条数据的位置在数据库中保存了
6,原理
我们的FoodItem类有一个position列,并且声明为acts_as_list,并且GroceryList按照position来排序,我们的sortable_element()这个helper方法为我们生成一段为Sortable.create(...)的JavaScript代码为我们做Ajax调用,访问sort方法,而sort方法则在数据库中保存列表经拖拽后的新的position。
分享到:
相关推荐
rails-angular-postgres-and-bootstrap-second-edition 英文原版
rails-ftw-v0.18-2.1.5-4.1.8.exe用于在windows环境下搭建readmine环境
从头开始创建Rails API 学习目标 创建仅API的Rails构建 介绍 在最后几节课中,我们了解了如何轻松调整Rails的MVC结构来呈现JSON。 Rails足够灵活,能够响应不同的格式,并且可以立即使用。 但是,出于在JavaScript和...
rails-hackernews-reddit-producthunt-clone, 黑客 news/reddit/social 链接分享网站 用 Rails 构建 Rails 上的 Reddit-Hackernews-ProductHunt克隆演示 这是一个 readme.md的Ruby on Rails 应用程序,模仿了 Hacker...
2-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏...
You should have some experience with basic Rails concepts and a cursory understanding of JavaScript, CSS, and SQL, but by no means need to be an expert. You'll learn how to install Postgres on your ...
Chapter 2 Creating a Component-Based Application Chapter 3 Tooling for Components Chapter 4 Component Refactorings:Extracting Components out of Components Chapter 5 From Ball of Mud to First ...
rails-documentation-2-0-2
Now in version 4, Rails is mature and powerful, and to use it effectively you need more than a few Google searches. You'll find no substitute for the guru's-eye-view of design, testing, deployment, ...
rails-documentation-1-2-1.zip
rails-documentation-1-2-0-rc1.chm
Rails, Angular, Postgres, and Bootstrap(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
jquery-ui+jquery-ui-rails
You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...
活动记录模型和导轨活动记录的作用Active Record是Rails用来管理应用程序模型方面的内置ORM。 什么是ORM? ORM是一个对象关系映射系统,从本质上讲,这是一个模块,它使您的应用程序能够以方法驱动的结构来管理数据...
活动记录模型和导轨活动记录的作用Active Record是Rails用来管理应用程序模型方面的内置ORM。什么是ORM? ORM是一个对象关系映射系统,从本质上讲,这是一个模块,它使您的应用程序能够以方法驱动的结构来管理数据。...
rails-beginner-s-guide是Rails 指导手册,帮组学习了解rails开发
Active Record是Rails用来管理应用程序模型方面的内置ORM。 什么是ORM? ORM是一个对象关系映射系统,从本质上讲,这是一个模块,它使您的应用程序能够以方法驱动的结构来管理数据。 这意味着您可以利用方法而不是...
Ajax-Rails-4-AJAX-modal-form-render-JS-response-as-table-row.zip,rails 4 ajax模式表单将js响应呈现为表行,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装