2012-12-11 2 views
0

사용자가 개인 컬렉션의 카테고리를 생성, 수정 및 추가 할 수있는 시스템을 개발하려고합니다. 사용자는 Personal Collecions 목록을 볼 수 있지만 개인 컬렉션에 항목을 추가하거나 수정하려면 계정을 만들어야하고 사용자는 로그인해야합니다.Ruby on Rails에서 로그인 폼을 만듭니다.

여기에서 RailsCast의 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"=>"[email protected]", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]"}, 
"commit"=>"Sign Up"} 

이 사용되는 컨트롤러의 소스 코드입니다.

users_controllers :

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 

Sessions_controller :

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 

application_controller :

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 
,174,515 모델에 대한

소스 코드 :

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 

이다 레이크 루트 폴더의 코드 :

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 

어떤 제안?

감사

+0

코드를 읽으십시오. –

+0

레이크 경로의 o/p를 게시 할 수 있습니까?/가입이 작동해야한다고 생각합니다. 밑줄을 제거하려고 ... – Bijendra

+0

고맙습니다. 페이지를 새로 고침 할 때 감사합니다. 다른 오류 메시지가 나타납니다 (위의 게시물 참조).). o/p는 무엇을 의미합니까? –

답변

0

사용자 모델에 대한 마이그레이션을 확인하십시오. password_digest에 대한 열이 있습니까? (열 유형은 string이어야합니다.)

+0

도움을 주셔서 감사합니다. password_digest를 추가하는 것을 잊었습니다. –

0

새의 DEF 블록 내에서 다음 줄을 추가하고 작동하는 경우 다음을 참조하십시오

할 respond_to | 형식 |

format.html # new.html.erb 
    format.json { render json: @user } 
end 
+0

도움 주셔서 감사합니다. 나는 그것을 시도하고 여전히 같은 오류 메시지가 나타납니다. 다른 제안이 있으십니까? –

+0

유 호스 컨트롤러 등에서 작성된 코드를 게시 할 수 있습니까? – Rinku

+0

예, 모든 컨트롤러를 추가했습니다. –