0

我正在尝试开发一个允许用户为其个人收藏创建、修改和添加类别的系统。用户可以查看个人收藏列表,但是要将项目添加或修改到他们的个人收藏中,必须创建一个帐户并且用户必须登录。

决定遵循来自 RailsCasts的https://github.com/ryanb/railscasts-episodes/tree/master/episode-250/revised/blog-after/app文件中的登录部分的源代码。

除了登录和注册表单外,其他一切都有效。

尝试加载页面时,我收到以下消息:

NoMethodError in UsersController#create

undefined method `password_digest=' for #<User:0x007fd8540db770>
Rails.root: /Users/laurens14/collections

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:14:in `new'
app/controllers/users_controller.rb:14:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"rqrGlYSK5eSl9+P3WHkSgazwi2zyQyJUiB/G9G6UOU4=",
 "user"=>{"email"=>"laurens14@icloud.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"},
 "commit"=>"Sign Up"}

这是所使用的控制器的源代码。

用户控制器:

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

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

会话控制器:

class SessionsController < ApplicationController
def new
end

def create
    user = User.find_by_email(params[:email])
    if user && user.authenticate(params[:password])
        session[:user_id] = user.id
        redirect_to root_url, notice: "Logged in!"
        else
        flash.now.alert = "Email or password is invalid"
        render "new"
    end
    end

def destroy
    session[:user_id] = nil
    redirect_to root_url, notice: "Logged out!"
end
  end

应用程序控制器:

 class ApplicationController < ActionController::Base
protect_from_forgery

private

def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

def authorize
    redirect_to login_url, alert: "Not authorized" if current_user.nil?
end
end

Collections_controller

   class CollectionsController < ApplicationController
   # GET /collections
   # GET /collections.json
   def index
   @collections = Collection.all

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

   def list
   end

    # GET /collections/1
   # GET /collections/1.json
  def show
  @collection = Collection.find(params[:id])

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

  # GET /collections/new
  # GET /collections/new.json
  def new
   @collection = Collection.new

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

    # GET /collections/1/edit
   def edit
   @collection = Collection.find(params[:id])
   end

def search
    @collections = Collection.find(:all, :conditions => ["title LIKE ?", "%#{params[:key]}%"])
end


   # POST /collections
   # POST /collections.json
  def create
@collection = Collection.new(params[:collection])

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

 # PUT /collections/1
# PUT /collections/1.json
def update
  @collection = Collection.find(params[:id])

  respond_to do |format|
   if @collection.update_attributes(params[:collection])
    format.html { redirect_to @collection, notice: 'Collection was successfully updated.'    }
    format.json { head :no_content }
    else
    format.html { render action: "edit" }
    format.json { render json: @collection.errors, status: :unprocessable_entity }
    end
   end
   end

  # DELETE /collections/1
 # DELETE /collections/1.json
 def destroy
   @collection = Collection.find(params[:id])
   @collection.destroy

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

模型的源代码:

user.rb

      class User < ActiveRecord::Base
      has_secure_password

        attr_accessible :email, :password, :password_confirmation

        validates_uniqueness_of :email
       end


collection.rb

        class Collection < ActiveRecord::Base
       attr_accessible :date, :description, :instructions, :title, :category_id
       belongs_to :category
     end


catergory.rb

          class Category < ActiveRecord::Base
          attr_accessible :name
          has_many :Collection
      end

这是 Rake 路由文件夹中的代码:

Collections::Application.routes.draw do
get 'signup', to: 'users#new', as: 'signup'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'

resources :users
resources :sessions

#resources :collections

resources :collections do
    post 'search', :on => :collection
    get 'list', :on => :collection


end

有什么建议么?

谢谢

4

2 回答 2

0

在 def 块中为 new 添加以下行,然后查看它是否有效:

respond_to 做 |格式|

  format.html # new.html.erb
  format.json { render json: @user }
end
于 2012-12-11T13:07:06.497 回答
0

检查用户模型的迁移。它有一个专栏password_digest吗?(列类型应该是string

于 2012-12-12T14:01:58.303 回答