-2
我創建了一個簡單的登錄認證應用程序,它實際上是一個Twitter的克隆。用戶登錄並訪問頁面等。RoR - NoMethodError,undefined方法
但是,當用戶從那裏發佈配置文件。它給出了一個錯誤
NoMethodError in RibbitsController#create
undefined method `id=' for nil:NilClass
的誤差約爲行5:
class RibbitsController < ApplicationController
def create
@ribbit = Ribbit.create(user_ribbits)
@ribbit.userid = current_user.id
if @ribbit.save
redirect_to current_user
else
flash[:error] = "Problem!"
redirect_to current_user
end
end
private
def user_ribbits
params.require(:ribbit).permit(:content, :userid)
end
end
給應用程序的請求:
Request
參數:
{"utf8"=>"✓",
"authenticity_token"=>"dwVmjDNO4GOowphGFgChMDBxBfvka+M/xSUHvJMECzwxtv4NF6OuWtiaX74NLz91OwQJ9T9+wm7yMiPQ0BLpGA==",
"ribbit"=>{"content"=>"hi. test.\r\n"},
"commit"=>"Ribbit!"}
的會話控制器:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_username(params[:username])
if user && user.authenticate(params[:password])
session[:userid] = user.id
redirect_to rooturl, notice: "Logged in!"
else
flash[:error] = "Wrong Username or Password."
redirect_to root_url
end
end
def destroy
session[:userid] = nil
redirect_to root_url, notice: "Logged out."
end
end
users控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.create(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to @user, notice: "Thank you for signing up!"
else
render 'new'
end
end
def show
@user = User.find(params[:id])
@ribbit = Ribbit.new
end
private
def user_params
params.require(:user).permit(:name, :username, :email, :password, :password_confirmation, :avatar_url)
end
end
和應用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
end
我真的很感激,如果你們能幫助!
謝謝。
你確定你的'Ribbit'被保存了嗎? –
不確定它是否是原因,但是您同時使用':userid'和':user_id'。可能想要堅持'user_id',因爲傳統上使用這種外鍵名稱 – lobanovadik
嘗試在'RibbitsContoller'的'create'動作中放置調試器,看看你是否在那裏獲得'current_user' – Deep