1
我有嵌套模型如下嵌套屬性在Rails的節省3.1
Project
模型has_many
關鍵字和關鍵字belongs_to
項目
class Project < ActiveRecord::Base
has_many :url_list
has_many :keyword
accepts_nested_attributes_for :keyword, :allow_destroy => true
end
class Keyword < ActiveRecord::Base
belongs_to :project
attr_accessible :kw, :project_id
end
查看:
<%=form_for @project, :multipart => true do |f|%>
<ul><li style = "width:85px; text-align: right; margin-right:5px; margin-top:5px"><%= f.label :name%></li>
<li><%= f.text_field :name, :class => "txt_box1"%></li></ul>
<ul><li style = "width:85px; text-align: right; margin-right:5px; margin-top:5px"><%= f.label :request_id %></li>
<li><%= f.text_field :request_id, :class => "txt_box1" %></li></ul>
<ul><li style = "width:85px; text-align: right; margin-right:5px; margin-top:5px"><%= f.label :file%></li>
<li><%= f.file_field :uploaded_file%></li></ul>
<%= f.fields_for :keywords do |k|%>
<ul><li style = "width:85px; text-align: right; margin-right:5px; margin-top:5px"><%= k.label "keyword1" %></li>
<li><%= k.text_field :kw, :class => "txt_box1" %></li></ul>
<%end%>
<ul><li style="width:85px; text-align: right; margin-right:5px; margin-top:5px"> </li>
<li style="opacity: 0.8;"><div class="space"><%= f.submit "Submit", :class=> "button"%></div></li></ul>
<%end%>
我不能救keywords
雖然這是節省project
我可能會出錯?
關鍵字圖
class CreateKeywords < ActiveRecord::Migration
def change
create_table :keywords do |t|
t.string :kw
t.integer :project_id
t.timestamps
end
end
end
欣賞你的迴應,幫助! – 2012-01-31 14:58:29