[英]rails 3 scaffolding of relational model
有沒有任何教程如何搭建一個使用多對多關系的簡單模型?
這是我在Rails 3.0.5上使用ruby 1.9.2逐步創建下面的testapp時編寫的。 另請參閱我使用的寶石的'Gemfile'(整個Testapp可下載,鏈接在第15部分的末尾)。 所以這里:
1)然后轉到你想要創建測試應用程序的地方
rails new mynewtestapp
cd mynewtestapp
2)然后添加2個具有has_and_belongs_to_many關聯的模型
rails g scaffold book title:string author:string
rails g scaffold user name:string age:integer
3)然后你需要為該asssociation創建連接表...默認情況下,rails將查找一個表,其名稱由字母順序的兩個關聯表組成...所以讓我們創建一個遷移來創建這樣的表
rails g migration createBooksUsers
4)打開生成的遷移文件,此時就是這樣
class CreateBooksUsers < ActiveRecord::Migration
def self.up
end
def self.down
end
end
5)修改它看起來像這樣
class CreateBooksUsers < ActiveRecord::Migration
def self.up
create_table :books_users, :id => false do |t|
t.integer :book_id
t.integer :user_id
end
end
def self.down
drop_table :books_users
end
end
6)將has_and_belongs_to_many關聯添加到書籍和用戶模型,以及關系添加的新ID
應用程序/模型/ book.rb
class Book < ActiveRecord::Base
attr_accessible :title, :author, :user_ids
has_and_belongs_to_many :users
end
應用程序/模型/ user.rb
class User < ActiveRecord::Base
attr_accessible: :name, :age, :book_ids
has_and_belongs_to_many :books
end
7)現在我們的模型和遷移完成了......讓我們創建表格
rake db:create
rake db:migrate
(如果你使用sqlite3或者如果你已經創建了手動使用的數據庫,那么可能沒有必要創建這個例子,因為我沒有添加任何與安裝數據庫管理系統相關的東西。但是因為有很多實際上所有值得使用的都有很好的記錄,你會發現任何有關這方面的幫助很快)
8)現在決定將哪個對象分配給哪個對象......當然,你可以做到這兩種方式......我會保持簡單,並向一個人展示......讓我們說你只有少數用戶和想把這些分配給書籍......
在這一點上,我會說讓我們得到一些外部幫助,比如二進制x建議......但為了簡單起見,我選擇了Simple_form gem而不是Formtastic。 我想每個人都有自己的最愛......但是simple_form似乎可以讓你更自由地根據自己的意願設計整個輸出...所以讓我們在這一點上安裝simple_form,就這么做
echo "gem 'simple_form', :git => 'git://github.com/plataformatec/simple_form.git'" >> Gemfile
將simple_form添加到Gemfile,然后運行
bundle install
並將簡單的表單安裝到您的應用程序(即生成配置,默認樣式和語言文件)
rails g simple_form:install
9)修改我們的書籍表格的時間
現在的書籍形式應該是這樣的
應用程序/視圖/書籍/ _form.html.erb
01 <%= form_for(@book) do |f| %>
02 <% if @book.errors.any? %>
03 <div id="error_explanation">
04 <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
05
06 <ul>
07 <% @book.errors.full_messages.each do |msg| %>
08 <li><%= msg %></li>
09 <% end %>
10 </ul>
11 </div>
12 <% end %>
13
14 <div class="field">
15 <%= f.label :title %><br />
16 <%= f.text_field :title %>
17 </div>
18 <div class="field">
19 <%= f.label :author %><br />
20 <%= f.text_field :author %>
21 </div>
22 <div class="actions">
23 <%= f.submit %>
24 </div>
25 <% end %>
使用simple_form,我們可以只替換上面的一些代碼(第1行和第14-24行),這樣整個文件看起來像這樣:
01 <%= simple_form_for(@book) do |f| %>
02 <% if @book.errors.any? %>
03 <div id="error_explanation">
04 <h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>
05
06 <ul>
07 <% @book.errors.full_messages.each do |msg| %>
08 <li><%= msg %></li>
09 <% end %>
10 </ul>
11 </div>
12 <% end %>
13
14 <%= f.input :title %>
15 <%= f.input :author %>
16 <%= f.association :users %>
17
18 <%= f.button :submit %>
19
20 <% end %>
10)現在您可能想要啟動您的應用程序
rails s
添加一些用戶,然后添加一本書,並且有第一個has_and_belongs_to_many表單:
11)那可能還不是最美的東西,但是簡單地添加樣式表會有所幫助...創建一個新文件
public/stylesheets/simple_form.css
並將以下行粘貼到其中
/* public/stylesheets/simple_form.css */
.simple_form label {
float: left;
width: 100px;
text-align: right;
margin: 2px 10px;
}
.simple_form div.input {
margin-bottom: 10px;
}
.simple_form div.boolean, .simple_form input[type='submit'] {
margin-left: 120px;
}
.simple_form div.boolean label, .simple_form label.collection_radio, .simple_form label.collection_check_boxes{
float: none;
margin: 0;
}
.simple_form .error {
clear: left;
margin-left: 120px;
font-size: 12px;
color: #D00;
display: block;
}
.simple_form .hint {
clear: left;
margin-left: 120px;
font-size: 12px;
color: #555;
display: block;
font-style: italic;
}
然后重新加載頁面...... Tadaa ......先攻......
12)如果您不喜歡多選列表框,請返回圖書表格
應用程序/視圖/書籍/ _form.html.erb
並修改行
15 <%= f.input :author %>
稍微
15 <%= f.input :author, :as => :check_boxes %>
從列表框中制作復選框......但是...... ewww ....看看這個:
13)有些東西似乎有點不對...已知這些選項的從左到右的表示會偶爾給simple_form greenhorns帶來麻煩,但實際上這是一個容易解決的問題
除了那個小格式問題之外,您可能還希望在大括號中看到他的名字背后的用戶年齡,例如'Tom(25)'
...所以我們做3個快速修復
a)取消注釋並在config / initializers / simple_form.rb中設置2個選項,以便用div包裝每個復選框並將一組復選框放在一個字段集中
# You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
config.collection_wrapper_tag = :fieldset
# You can wrap each item in a collection of radio/check boxes with a tag, defaulting to none.
config.item_wrapper_tag = :div
b)稍微修改一下simple_form.css樣式表,如添加:
fieldset { border: 0; }
...除非你更喜歡圍繞場地設置的大丑陋邊框
c)在我們的用戶模型中創建方法'to_label',因為'to_label'默認是simple_form查找的第一個方法,以便獲得一個String表示來顯示一個對象。 通過一個奇怪的事件,我們的模型用戶有一個名為“名稱”的列。 正如名稱也是一個方法simple_form在模型中尋找,我們很幸運這個應用程序到目前為止工作。 如果我們調用名稱列forename,Rails將列出不是用戶名而是default-ruby-object表示(例如<#User:521369846>)。 猜猜我們很幸運;-)
應用程序/模型/ user.rb
class User < ActiveRecord::Base
has_and_belongs_to_many :users
def to_label
"#{name} (#{age})"
end
end
編輯表格看起來很漂亮......
14)現在只有節目視圖需要顯示書籍所有者...這也不太難,只需打開節目視圖
應用程序/視圖/書籍/ show.html.erb
並添加第13-16行以顯示書籍所有者:
01 <p id="notice"><%= notice %></p>
02
03 <p>
04 <b>Title:</b>
05 <%= @book.title %>
06 </p>
07
08 <p>
09 <b>Author:</b>
10 <%= @book.author %>
11 </p>
12
13 <p>
14 <b>Who owns a copy?</b>
15 <%= @book.users.map {|x| x.to_label}.join ', ' %>
16 </p>
17
18 <%= link_to 'Edit', edit_book_path(@book) %> |
19 <%= link_to 'Back', books_path %>
最后但並非最不重要的......展示視圖
15)嗯,非常需要快速的教程來habtm或者在rails中使用has_and_belongs_to_many關聯。 我在網上寫這個我創建的測試應用程序, 網址是https://1drv.ms/u/s!Alpu50oGtUZq7AiJkL08QqBiMAjb
觀看Ryan Bate關於Token Fields的教程:
http://railscasts.com/episodes/258-token-fields
這是一種在rails中支持多對多關系的簡單方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.