2

我在第 7 章的 Railstutrial 中尝试添加用户头像。

我尝试使用回形针让用户上传他们自己的头像,而不是使用默认的书籍指南在 railstutorial 第 7 章中使用 gravatar
。 所以我找到了这个教程并做了这些事情,直到:
这是数据库迁移([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

这是用户控制器(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.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

显示 /home/[username]/rails_projects/final_project/app/views/users/new.html.erb 其中第 6 行提出:

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| %>  

我认为这应该是冲突的根源!


更新

当我将 th 添加:url => user_path到 form_for 助手时,现在它给了我以下错误:

路由错误
没有路由匹配 {:action=>"show", :controller=>"users"}

我认为将表单数据发布到服务器后无法进入用户页面!

4

1 回答 1

1

应该是users_path,不是user_path

编辑

users_path        =>  /users     # This URL gets routed to users#create for a
                                 # POST method and users#index for a GET method

user_path(@user)  =>  /users/55  # This URL gets routed to users#show for a GET
                                 # method and users#destroy for a DELETE method
于 2013-06-11T12:18:50.403 回答