VPNTIME
VPNTIME

Reputation: 761

rails error-- undefined method `model_name' for NilClass:Class

I'm new to rails development. This is my first app. And I've been struggling through it. Anyhow this is my issue. My modelname is PostIt and my controller is post_it_controller.rb

class PostItController < ApplicationController

def create
 @user = User.find(params[:id])
 @posts = Postit.create(params[:content])
 @posts.receiver_id = params[:content][:receiver_id]
 @posts.sender_id = current_user.id
end

and in my views I have this in create.html.erb

<%= form_for(@posts) do |f| %>  
<%= f.hidden_field :receiver_id, :value => @user %>  
<%= f.label :content %>  
<%= f.text_area :content, :class => 'inputbox' %>  
<%= f.submit "Submit", :class => 'btn right' %>
<% end %>

the error I keep getting is undefined method `model_name' for NilClass:Class

Upvotes: 0

Views: 125

Answers (1)

James
James

Reputation: 4737

There are a few things in your code that break convention.

  1. The create action is normally called when you submit the form with a POST request.
  2. Your form should be rendered in a new.html.erb or edit.html.erb.
  3. Your controllers should be named with the plural form. i.e. PostItsController.

With that said, I think the error is due to @posts being nil. This may be because @posts is being assigned in the wrong controller action, hence not being assigned at all by the time the response renders the form.

Upvotes: 1

Related Questions