I was on Railstutrial on chapter 7 where it tries to add user avatars.
I tried to use paperclip to let users upload their own avatar, instead of using default book guid on using gravatar in railstutorial, chapter 7.
So I found this tutrial and did the stuff until:
This is the DB Migration ([timestamp]_add_attachments_avatar_to_user.rb)
class AddAttachmentsAvatarToUser < ActiveRecord::Migration
def self.up
add_column :users, :avatar_file_name, :string
add_column :users, :avatar_content_type, :string
add_column :users, :avatar_file_size, :integer
add_column :users, :avatar_updated_at, :datetime
end
def self.down
remove_column :users, :avatar_updated_at
remove_column :users, :avatar_file_name
remove_column :users, :avatar_content_type
remove_column :users, :avatar_file_size
end
end
This is UsersController (users_controller.rb)
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to My Site"
redirect_to @user
else
render 'new'
end
end
end
This is User Model (user.rb)
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation,
:avatar,
:avatar_file_name,
:avatar_content_type,
:avatar_file_size,
:avatar_updated_at
has_secure_password
has_attached_file :avatar, :styles => { :large => "120x120>", :medium => "48x48>", :thumb => "26x26>" }
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
This is Routes file (routes.rb)
FinalProject::Application.routes.draw do
resources :users
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/terms', to: 'static_pages#terms'
end
This is the form in my view (new.html.erb) Updated
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for @user, :html => { :multipart => true } do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.label 'avatar' %>
<%= f.file_field :avatar %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
and finally this is the error, when I try to signup a new user:
NoMethodError in Users#new
Showing /home/[username]/rails_projects/final_project/app/views/users/new.html.erb where line #6 raised:
undefined method `model_name' for NilClass:Class
Please Help!
UPDATED
This is the form helper the auther proposed on reinventar.com:
<% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |f| %>
Where as I decided to use this:
<%= form_for @user, :html => { :multipart => true } do |f| %>
I Think this should be the source of conflict!
UPDATED
As I added th :url => user_path
to the form_for helper, now It is giving me the following error:
Routing Error
No route matches {:action=>"show", :controller=>"users"}
I think It can not go to user page after posting form data to server!