Rails 3 - 从索引中呈现创建表单

时间:2011-08-08 23:48:26

标签: ruby-on-rails-3

我有以下形式partial(contacts / _form.html.erb)

<%= 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 %>

我正在尝试从contacts / index.html.erb

呈现它
<%= render :partial => 'form'%>

但是我得到错误......

undefined method `user' for nil:NilClass

2 个答案:

答案 0 :(得分:0)

您需要在联系人#index action内初始化@contact

答案 1 :(得分:0)

发生的事情是一个愚蠢的错误。

我正在加载CANCAN并授权资源,然后我指定了@contacts =我的索引视图的任何内容,它正在拧紧cancan up并且它不再自动生成该操作的实例变量。

无论哪种方式修复都是写出来而不是让cancan自动生成它......

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