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

ユーザー モデルの移行を確認します。の列はありpassword_digestますか? (列タイプは である必要がありますstring)

于 2012-12-12T14:01:58.303 に答える
0

new の def ブロック内に次の行を追加し、動作するかどうかを確認します。

Respond_to do |フォーマット|

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