iamdhunt
iamdhunt

Reputation: 453

Rails undefined method `model_name' for NilClass:Class When trying to render simple_form in a view

I've searched through the questions and couldn't find an answer that would work for me and I'm new to Rails and am stuck. I'm trying to render a simple_form I use to post statuses into a view for user profiles. But every time I load the profile page I get the following error:

undefined method `model_name' for NilClass:Class

Extracted source (around line #1):

1: <%= simple_form_for(@status) do |f| %>

I don't know why because it renders fine in two other views I have, only difference they are located in the same folder as the form. The profile is not. This is what I used to render the form:

<%= render partial: "statuses/form", locals: { status: @status} %>

and here's what my controller looks like:

class StatusesController < ApplicationController

  before_filter :authenticate_member!, only: [:new, :create, :edit, :update] 

  # GET /statuses
  # GET /statuses.json
  def index
  @statuses = Status.all

    respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @statuses }
   end
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
    @status = Status.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/new
  # GET /statuses/new.json
  def new
    @status = Status.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @status }
    end
  end

  # GET /statuses/1/edit
  def edit
    @status = Status.find(params[:id])
  end

  # POST /statuses
  # POST /statuses.json
  def create
    @status = current_member.statuses.new(params[:status])

    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render json: @status, status: :created, location: @status }
      else
        format.html { render action: "new" }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /statuses/1
  # PUT /statuses/1.json
  def update
    @status = current_member.statuses.find(params[:id])
    if params[:status] && params[:status].has_key?(:user_id)
        params[:status].delete(:user_id) 
    end 
    respond_to do |format|
      if @status.update_attributes(params[:status])
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /statuses/1
  # DELETE /statuses/1.json
  def destroy
    @status = Status.find(params[:id])
    @status.destroy

    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end
end

What am I missing here?

Upvotes: 0

Views: 1846

Answers (2)

iamdhunt
iamdhunt

Reputation: 453

Ok I finally figured this one out. Instead of trying to render it I just added to form directly to view like so:

<%= simple_form_for(@status) do |f| %>
  <%= f.input :content, label: false, input_html: { class: 'forms', id: 'update_box', tabindex: '1', rows: '1' } %>
  <span class="actions">
   <%= f.submit "Post to Stream", :class => 'reg_button btn btn-info' %>
  </span>
<% end %>

and added @status = Status.new to my show method in my Profiles Controller so that it posts correctly:

class ProfilesController < ApplicationController

  def show
    @status = Status.new
    @member = Member.find_by_user_name(params[:id])
    if @member 
        @statuses = @member.statuses.all
        render action: :show
    else
        render file: 'public/404', status: 404, formats: [:html]
    end
  end

end

Hope this helps someone else out.

Upvotes: 0

user229044
user229044

Reputation: 239240

The local variable in the partial will be status, not @status.

Upvotes: 1

Related Questions