2013-07-15 30 views
1

我跟着這個Rails tutorial描述如何做嵌套模型表單。在4:32,他開始描述如何用三個空模型預先填充表單。這兩種型號的問題是:Rails:沒有什麼產生3.times {model1.model2.build}

class Event < ActiveRecord::Base 
    has_many :positions, dependent: :destroy 
    accepts_nested_attributes_for :positions 
end 

而且......

class Position < ActiveRecord::Base 
    belongs_to :event 
end 

在我的事件控制器,我加入了教程的代碼爲new方法

def new 
    @event = Event.new 
    3.times { @event.positions.build } 
end 

我的活動的形式視圖也被填充。

<!-- /apps/views/events/_form.html.erb --> 

<%= form_for(@event) do |f| %> 
    <h3>Event Details</h3> 

    <div class="field"> 
    <%= f.label :name %><br> 
    <%= f.text_field :name %> 
    </div> 
    <!-- more fields here --> 

    <h3>Create positions for the event</h3> 
    <% f.fields_for :positions do |builder| %> 
    <p> 
     <%= builder.label :name %> 
     <%= builder.text_field :name %> 
    </p> 
    <!-- more fields here --> 
    <% end %> 

    <!-- more fields here --> 
<% end %> 

但是,position字段沒有出現在我的表單上。我rake db:migrated並重新啓動服務器(Ctrl-Crake s)一堆次沒有效果。我究竟做錯了什麼?

+0

是'@ event.positions.build'的方法? '@ event.positions.build()'怎麼樣? – Automatico

+0

是一種ActiveRecord方法。在Ruby中你不需要()。 – cortex

+0

railscast沒有parens。我添加了它們,並沒有影響任何東西。 – Jeff

回答

1

你錯過了attr_accessible的東西。

在你的事件添加到模型:

attr_accessible :positions_attributes 

這個例子看看:

class Contact < ActiveRecord::Base 
    attr_accessible :addresses_attributes, :birth_date, :email, :gender, :name, :vat_number 
    has_many :addresses 
    accepts_nested_attributes_for :addresses 
    # validates_uniqueness_of :vat_number 
    paginates_per 50 

    def gender=(gender) 
    gender = gender.downcase 
    case gender.downcase 
    when 'm' 
     gender = 'male' 
    when 'f' 
     gender = 'female' 
    else 
    end 
    write_attribute(:gender, gender) 
    end 
end 


class Address < ActiveRecord::Base 
    attr_accessible :city, :country, :postal_code, :street, :contact 
    belongs_to :contact 
end 

而且,由於你是以下該教程,你可能會在助手停止。這裏有一個工作版本爲3.2.13導軌:

def link_to_remove_fields(name, f) 
    f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)", :class => "icon-remove") 
    end 

    def link_to_add_fields(name, f, association) 
    new_object = f.object.class.reflect_on_association(association).klass.new 
    fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| 
     render(association.to_s.singularize + "_fields", :f => builder) 
    end 
    link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")") 
    end 

和JavaScript助手:

function remove_fields(link) { 
    $(link).prev("input[type=hidden]").val("1"); 
    $(link).closest(".fields").hide(); 
} 

function add_fields(link, association, content) { 
    var new_id = new Date().getTime(); 
    var regexp = new RegExp("new_" + association, "g"); 
    $(link).parent().before(content.replace(regexp, new_id)); 
    $(link).parent().append('<input type="hidden" name="click" value="true" />'); 
    $("#last_id").val(new_id); 
} 
+0

我最初並沒有使用'attr_accessible'。我嘗試添加它,但它沒有改變。其餘的代碼看起來很有趣。這是用JavaScript添加新的兒童模型嗎? – Jeff

+0

我最終用新的v4強大的參數去結束工作。謝謝! 'params.require(:event).permit(...,positions_attributes:[:id,:name,:description])' – Jeff

相關問題