我正在关注教程:https://www.youtube.com/watch?v=7-1HCWbu7iU 我已经使用设计宝石做了Pintrest克隆并没有问题。当我尝试将current_user添加到链接控制器时,我得到了未定义的方法current_user"。我尝试了一切......甚至从头开始重做应用程序,定义current_user,重置数据库。我猜宝石应该自动处理这个方法,对吗?
以下是我的步骤:
1. Generate new app - rails new raddit
2. Generate scaffold: rails g scaffold link title:string url:string
3. rake db:migrate
4. add gem 'devise', '~> 3.3.0' to gem file and run bundle install
5. run rails g devise:install
6. add file to development.rb - config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
7. add alerts to layout/application.html (as per devise documentation)
8. generate view file - rails g devise:views
9. generate user - rails g devise User
10. rake db:migrate
11. rails s
12. go to localhost:3000/users/sign_up (works ok)
13. Add relation to models: user.rb - has_many :links ; link.rb - belongs_to :user
14. Create migration: rails generate migration add_user_id_to_links user_id:integer:index
15. rake db:migrate
16. go to rails console and save assocation between user and links.
17. Add current_user to links controller - Getting undefined 'curren_user method' once I go to localhost:3000/links/new
这是我的代码:
class LinksController < ApplicationController
before_action :set_link, only: [:show, :edit, :update, :destroy]
def index
@links = Link.all
end
def show
end
def new
@link = Link.current_user.links.build
end
def edit
end
def create
@link = current_user.build(link_params)
respond_to do |format|
if @link.save
format.html { redirect_to @link, notice: 'Link was successfully created.' }
format.json { render :show, status: :created, location: @link }
else
format.html { render :new }
format.json { render json: @link.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @link.update(link_params)
format.html { redirect_to @link, notice: 'Link was successfully updated.' }
format.json { render :show, status: :ok, location: @link }
else
format.html { render :edit }
format.json { render json: @link.errors, status: :unprocessable_entity }
end
end
end
def destroy
@link.destroy
respond_to do |format|
format.html { redirect_to links_url, notice: 'Link was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_link
@link = Link.find(params[:id])
end
def link_params
params.require(:link).permit(:title, :url)
end
end
我的模特:link.rb
class Link < ActiveRecord::Base
belongs_to :user
end
我的模特:user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :links
end
答案 0 :(得分:3)
您正在调用Link.current_user的新操作中获取未定义的方法“current_user”。
var maxTotal = 1500; //for you 8000
var objects = new List<Item>()
{
new Item() {Score = 10, Price = 100},
new Item() {Score = 20, Price = 800},
new Item() {Score = 40, Price = 600},
new Item() {Score = 5, Price = 300},
};
decimal runningTotal = 0;
var newList = objects
.OrderByDescending(p => p.Score)
.Select(p =>
{
runningTotal = runningTotal + p.Price;
return new ItemWithRunningTotal()
{
Item = p,
RunningTotal = runningTotal
};
})
.OrderByDescending(p => p.RunningTotal)
.Where(p => p.RunningTotal <= maxTotal).Take(15);
应该是
def new
@link = Link.current_user.links.build
end
链接没有current_user方法,这就是问题所在。 current_user在控制器的上下文中定义,应该像在create操作中一样使用。
答案 1 :(得分:0)
应该是
@link = current_user.links.build
采取新行动
如果您遇到的问题不仅仅是在新操作中输入exit并检查控制台中的current_user ..您可以从控制台获取错误..所以每次都要检查您的控制台。
使用better_errors gem ..了解有关特定错误的更多信息