Cornelius Wilson
Cornelius Wilson

Reputation: 2914

Undefined method for edit page

I am new to Rails. My new.html.erb file works perfect as it shows at http://localhost:3000/signup. However I can't seem to get /edit to work. I receive this error:

undefined method `model_name' for NilClass:Class
Extracted source (around line #3):

1: <h1>Account Information</h1>
2: 
3: <%= form_for @user do |f| %>
4:   <% if @user.errors.any? %>
5:     <div class="error_messages">
6:       <h2>Form is invalid</h2>

Here's my edit.html file which is a replica of the new.html that works. I tried removing the error messages code and it still just displayed another error with the page.

<h1>Account Information</h1>

<%= form_for @user do |f| %>
  <% if @user.errors.any? %>
    <div class="error_messages">
      <h2>Form is invalid</h2>
      <ul>
        <% @user.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

    <div class="field">
        <%= f.label :email %><br/>
        <%= f.text_field :email %>
    </div>
    <div class="field">
        <%= f.label :password %><br/>
        <%= f.password_field :password %>
    </div>
    <div class="field">
        <%= f.label :password_confirmation %><br/>
        <%= f.password_field :password_confirmation %>
    </div>
    <div class="field">
        <%= f.label :username %><br/>
        <%= f.text_field :username %>
    </div>
    <div class="field">
        <%= f.label :zip_code %><br/>
        <%= f.text_field :zip_code %>
    </div>
    <div class="field">
        <%= f.label :birthday %><br/>
        <%= f.text_field :birthday %>
    </div>  
    <div class="actions"><%= f.submit %></div>
<% end %>

Here's my users_controller that I'm not sure if you need to look at or not. Maybe I have the def edit part wrong.

   class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.registration_confirmation(@user).deliver
      session[:user_id] = @user.id
      redirect_to root_url, notice: "Thank you for signing up!"
    else
      render "new"
    end

    def edit
      @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:user])
    if @user.update_attributes(params[:user])
      flash[:success] = "Account updated"
      sign_in @user
      redirect_to @user
    else
      render 'edit'
  end
end
end
end

Upvotes: 0

Views: 296

Answers (1)

Chowlett
Chowlett

Reputation: 46667

Your code indentation is a tell-tale sign here; you're defining the edit and update methods inside def create; the end immediately prior closes the if @user.save, not the def create.

Upvotes: 1

Related Questions