Rails 3 - rendering the create form from the index
I have the following form 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 =&g开发者_如何学Got; @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 →") %>
<% else %>
<%= button_submit_tag raw("Create Contact →") %>
<% end %>
<% end %>
<% end %>
I am trying to render it from contacts/index.html.erb
<%= render :partial => 'form'%>
But i get the error...
undefined method `user' for nil:NilClass
You need to initialize @contact
inside of your contacts#index action.
What was happening was a stupid mistake.
I was having CANCAN load and authorize the resource, then i was specifying the @contacts = whatever for my index view, and it was screwing cancan up and it wasn't auto generating the instance variables for that action anymore.
Either way the fix was writing it out instead of letting cancan auto-generate it...
@user = User.find(params[:user_id])
@contact = @user.contacts.build
精彩评论