我正在嘗試開發一個允許用戶爲其個人集合創建,修改和添加類別的系統。用戶可以查看個人收藏列表,但要添加或修改要創建的個人收藏的個人收藏,並且用戶必須登錄。在Ruby on Rails中創建登錄表單
決定從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"=>"[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
爲模特的源代碼:
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
有什麼建議?
感謝
嘗試閱讀您的代碼。 –
可以發佈耙路徑的o/p ...我認爲/註冊應該工作..嘗試刪除下劃線... – Bijendra
謝謝,我刪除了下劃線和刷新頁面時收到不同的錯誤信息(請參閱上面的帖子)。 o/p是什麼意思? –