Railstutrial の第 7 章で、ユーザーのアバターを追加しようとしました。
Railstutorialの第 7 章で gravatarを使用する際にデフォルトのブック GUID を使用する代わりに、ペーパークリップを使用して、ユーザーが自分のアバターをアップロードできるようにしようとし
ました
。 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
これは、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
これはユーザー モデル (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
これは Routes ファイル (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
これは私のビューのフォームです (new.html.erb)更新済み
<% 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>
最後に、新しいユーザーにサインアップしようとすると、これがエラーになります。
ユーザーの NoMethodError#new
6 行目が発生した /home/[username]/rails_projects/final_project/app/views/users/new.html.erb を表示:
NilClass:Class の未定義メソッド `model_name'
助けてください!
更新しました
これは、著者が reinventar.com で提案したフォーム ヘルパーです。
<% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |f| %>
私がこれを使用することに決めた場所:
<%= form_for @user, :html => { :multipart => true } do |f| %>
私はこれが紛争の原因であるべきだと思います!
更新しました
:url => user_path
form_for ヘルパーにth を追加すると、次のエラーが表示されます。
ルーティング エラー
{:action="show", :controller="users"} に一致するルートがありません
フォームデータをサーバーに投稿した後、ユーザーページに移動できないと思います!