2011-08-08 23 views
0

我有以下形式的部分(聯繫人/ _form.html.erb)的Rails 3 - 渲染從索引中創建表單

<%= semantic_form_for [@contact.user, @contact], :remote => true do |f| %> 
<% f.inputs do %> 
    <%= f.input :firstname, :label => 'First Name' %> 
    <%= f.input :lastname, :label => 'Last Name' %> 
    <%= f.input :email, :label => 'Email' %> 

    <%= f.input :groups, :collection => @user.groups, :as => :check_boxes, :label => 'Groups' %> 

    <%= f.input :notes, :input_html => { :class => 'autogrow', :rows => 10, :cols => 50, :maxlength => 10 }, :label => 'Notes' %> 
<% end %> 


<%= f.buttons do %> 
    <% if ["edit", "update"].include? params[:action] %> 
     <%= button_submit_tag raw("Update Contact &rarr;") %> 
    <% else %> 
     <%= button_submit_tag raw("Create Contact &rarr;") %> 
    <% end %> 
<% end %> 

<% end %> 

我試圖從聯繫人/ index.html.erb

使它
<%= render :partial => 'form'%> 

,但我得到的錯誤...

undefined method `user' for nil:NilClass 

回答

0

發生了什麼是一個愚蠢的錯誤。

我在使用CANCAN加載並授權資源,然後我爲我的索引視圖指定了@contacts = whatever,並且它使得cancan up,並且它不再自動爲該操作生成實例變量。

無論哪種方式修復被寫出來,而不是讓康康舞自動生成它......

@user = User.find(params[:user_id]) 
@contact = @user.contacts.build 
0

需要初始化@contact聯繫人#index動作的內部。